I have a Facility Booking System and I would like to create a CSV format report.
1. I want to count total booking records
2. I want to count how many user booked our facilities, based on that timeslot
3. I want to combine two queries into one as I need to put the result into the CSV
And here is the table structure:
fbid | memberid | fbdate | fbtimeslot | fname | fposition | fattendance
------------------------------------------------------------------------------
1 | C00001 | 2014-12-12 | 1 | computer | 1 | 1
2 | C00002 | 2014-12-12 | 1 | computer | 4 | 3
3 | C00003 | 2014-12-12 | 1 | computer | 6 | 1
4 | C00002 | 2014-12-12 | 1 | computer | 8 | 3
5 | C00004 | 2014-12-12 | 1 | computer | 4 | 0
6 | C00002 | 2014-12-12 | 1 | computer | 24 | 1
7 | C00001 | 2014-12-12 | 2 | computer | 1 | 0
8 | C00002 | 2014-12-12 | 3 | computer | 1 | 0
For task 1, I have found the solution:
(fattendance = 3 means the position has changed and I have to keep record but the CSV report doesn't this)
SELECT fbtimeslot, COUNT(*) FROM facilitybooking WHERE fname='computer' AND fattendance<>3 GROUP BY fbtimeslot
But for 2 and 3, I tried more than 20 different statements but I still couldn't get the result.
Well, hard tasks though.
I will be very appreciated if you can help me to solve this.
Extras: SQLFiddle Link: http://sqlfiddle.com/#!2/4800b8/3
Related
I wanted to make a learning apps with a completed questions progress track on each level. I have 2 tables which contain all the level with its question count, and a table that save an user progress (completed questions) on each level. Here is the record,
Level table:
+----------+----------------------+-------------+--------+
| id_level | name | jumlah_soal | id_sub |
+----------+----------------------+-------------+--------+
| 1 | Basic Level 1 | 5 | 1 |
| 2 | Basic Level 2 | 6 | 1 |
| 3 | Basic Level 3 | 7 | 1 |
| 8 | Intermediate Level 1 | 5 | 2 |
| 9 | Intermediate Level 2 | 5 | 2 |
| 10 | Intermediate Level 3 | 5 | 2 |
..........................................................
Progress Table:
+-----------------+---------+-------------+----------+
| id_progreslevel | id_user | completed | id_level |
+-----------------+---------+-------------+----------+
| 1 | 1 | 2 | 1 |
| 2 | 1 | 3 | 2 |
| 3 | 2 | 2 | 1 |
+-----------------+---------+-------------+----------+
and when I joined the tables with the following query
SELECT IFNULL(progreslevel.id_user, 1) as id_user,
-> level.*, IFNULL(progreslevel.completed, 0) as completedquestions
-> FROM level LEFT JOIN progreslevel
-> ON level.id_level = progreslevel.id_level
-> WHERE level.id_sub = 1
-> HAVING id_user = 1;
It queries what I wanted:
+---------+----------+---------------+-------------+--------+--------------------+
| id_user | id_level | name | jumlah_soal | id_sub | completed questions |
+---------+----------+---------------+-------------+--------+--------------------+
| 1 | 1 | Basic Level 1 | 5 | 1 | 2 |
| 1 | 2 | Basic Level 2 | 6 | 1 | 3 |
| 1 | 3 | Basic Level 3 | 7 | 1 | 0 |
+---------+----------+---------------+-------------+--------+--------------------+
BUT, when I tried to change to query a progress for user ID = 2, it became like this:
+---------+----------+---------------+-------------+--------+--------------------+
| id_user | id_level | name | jumlah_soal | id_sub | completed questions |
+---------+----------+---------------+-------------+--------+--------------------+
| 2 | 1 | Basic Level 1 | 5 | 1 | 2 |
| 2 | 3 | Basic Level 3 | 7 | 1 | 0 |
+---------+----------+---------------+-------------+--------+--------------------+
Yes, the Basic Level 2 is gone because user 2 hadn't done it yet but user 1 had.
This is where I'm stuck, I want to whichever user I choose, its always query all the level, even when the other user had done it. It should be like this:
+---------+----------+---------------+-------------+--------+--------------------+
| id_user | id_level | name | jumlah_soal | id_sub | completed questions |
+---------+----------+---------------+-------------+--------+--------------------+
| 2 | 1 | Basic Level 1 | 5 | 1 | 2 |
| 2 | 2 | Basic Level 2 | 6 | 1 | 0 |
| 2 | 3 | Basic Level 3 | 7 | 1 | 0 |
+---------+----------+---------------+-------------+--------+--------------------+
How do I achieve this?
Thanks in advance and Sorry if you got dizzy either at my explanation or database, I tried my best to translate it, so it's understandable
I'm not really sure that I understand the requirement, but I think you're after something like this...
SELECT l.*
, 2 id_user
, COALESCE(MAX(p.completed),0) completed_questions
FROM level l
LEFT
JOIN progress p
ON p.id_level = l.id_level
AND p.id_user = 2
WHERE l.id_sub = 1
GROUP
BY l.id_level;
So I am building a webpage that shows a bunch of video games located in a SQL database and one suggestion I had was to have the different prices from each region display based on a drop down. My question is trying to figure out whats the best way to store int in the database. Would it be like:
GAME
CountryID
price1
CountryID
price2
CountryID
price3 ...
Or is there a better way to do this?
Just a heads up I've only been developing web applications for a year or so and I'm still pretty new to SQL.
Thanks for your input!
I would use multiple tables, one for games and one for region pricing.
Games
+--------+----------+
| GameID | GameName |
+--------+----------+
| 1 | Game1 |
| 2 | Game2 |
| 3 | Game3 |
| 4 | Game4 |
+--------+----------+
RegionPricing
+----------+--------+-------+
| RegionID | GameID | Price |
+----------+--------+-------+
| 1 | 1 | 60 |
| 1 | 2 | 55 |
| 1 | 3 | 45 |
| 1 | 4 | 80 |
| 2 | 1 | 50 |
| 3 | 2 | 30 |
| 3 | 3 | 25 |
| 3 | 4 | 45 |
| 4 | 1 | 60 |
| 4 | 2 | 55 |
| 4 | 3 | 45 |
| 4 | 4 | 80 |
+----------+--------+-------+
By using separate tables you minimize duplicate data and allow for easy granular changes. You may also consider adding a column to RegionPricing for currency. This would also need a Region table, with RegionID and RegionName.
I have a table that looks like this if I 'select *'
+----+--------+------+------------+
| id | name | task | day |
+----+--------+------+------------+
| 1 | Rodney | 2 | 2016-05-05 |
| 2 | Rodney | 2 | 2016-05-08 |
| 3 | Rodney | 8 | 2016-05-08 |
| 4 | Scott | 2 | 2016-05-05 |
| 5 | Scott | 8 | 2016-05-05 |
| 6 | Frank | 2 | 2016-05-05 |
| 7 | Frank | 2 | 2016-05-08 |
+----+--------+------+------------+
What I'm trying to achive is a query that will get the last entered 'task' for each person. So, in this case I would want back:
2 | Rodney | 2 | 2016-05-08
3 | Rodney | 8 | 2016-05-08
4 | Scott | 2 | 2016-05-05
5 | Scott | 8 | 2016-05-05
7 | Frank | 2 | 2016-05-08
I'm pretty sure I need to use distinct against name & task and max for the most recent entry. Just not sure how to structure the two of them together to get the result.
select distinct name, task from test;
Gets me close...
+--------+------+
| name | task |
+--------+------+
| Rodney | 2 |
| Rodney | 8 |
| Scott | 2 |
| Scott | 8 |
| Frank | 2 |
+--------+------+
But no date...My SQL is limited. Any help would be appreciated.
Aggregate your rows so as to get the latest day per name. Then access the table again to get the records matching thse days:
select *
from test
where (name, day) in
(
select name, max(day)
from test
group by name
);
Another way is to select the records for which not exists a later record for the same name:
select *
from test
where not exists
(
select *
from test later
where later.name = test.name
and later.day > test.day
);
I am trying to group a record only if two of the fields repeat themselves.
I am designing a social sharing photo app. users can share, like and comment on thers photo. Each action (share, comment, like) will appear on their friends wall.
The Problem is that when a user do all the three actions, the picture appears three times instead of one with the three action on it.
Data in database is like this (activities_tb)
id | photoID | uiID | action | date
-------------------------------------------
1 | 1 | 2 | like | 01/01/2015
2 | 1 | 2 | share | 02/01/2015
3 | 1 | 4 | share | 03/01/2015
4 | 1 | 2 | comment | 04/01/2015
5 | 2 | 4 | like | 04/01/2015
6 | 2 | 2 | like | 05/01/2015
7 | 2 | 3 | share | 05/01/2015
8 | 2 | 4 | comment | 06/01/2015
8 | 3 | 3 | like | 07/01/2015
9 | 3 | 5 | like | 08/01/2015
10 | 3 | 5 | comment | 08/01/2015
The query result I want to get
id | photoID | uiID | action | date
-------------------------------------------
3 | 1 | 4 | share | 03/01/2015
4 | 1 | 2 | comment | 04/01/2015
6 | 2 | 2 | like | 05/01/2015
7 | 2 | 3 | share | 05/01/2015
8 | 2 | 4 | comment | 06/01/2015
8 | 3 | 3 | like | 07/01/2015
10 | 3 | 5 | comment | 08/01/2015
This is my statement
SELECT id, photoID, uiID, action, date
FROM activities_tb
GROUP BY photoID, uiID.
This combines all the photos by their id returning only three results
I will be glad if anyone can be of help, thank you
You can first select required ids and join on your table:
select tb.*
from activities_tb tb
join(select max(id) as id
from activities_tb
group by photoID, uiID) t on t.id = tb.id
You are looking for "SELECT DISTINCT"
SELECT DISTINCT photoID, uiID, action, date
FROM activities_tb
GROUP BY photoID, uiID.
I had a technical interview last week, and my interviewer asked me what happens if I run the following query:
SELECT * FROM tbl1, tbl2
I think I answered it correctly, but it wasn't an in-depth answer.
I said that I would select all the columns in both tables. For example if tbl1 has 3 columns, and tbl2 has 4 columns. The result set would have 7 columns.
Then he asked me why 7? and I said because I was selecting everything from each table.
That was a bad answer, but I couldn't think of anything else.
To cut to the chase, after the interviewed I executed the latter statement using two tables.
Table A, had 3 animal: dog, cat and elephant.
Table B had 2 names: Mat and Beth
This is the result set that I got after the statement being executed:
*********************************************
| id_tbl1 | name_tbl1 | id_tbl2 | name_tbl2 |
*********************************************
| 1 | dog | 1 | Mat |
| 2 | cat | 1 | Mat |
| 3 | elephant | 1 | Mat |
| 1 | dog | 2 | Beth |
| 2 | cat | 2 | Beth |
| 3 | elephant | 2 | Beth |
*********************************************
So my question is, why does the statement behaves like that?
In other words:
Why does the Table B's records repeat themselves until I reach the end of table A, and then it starts all over again?
How would you have answered the question in a way that it would've "WOW'd" the interviewer?
If this question does not belong to SO, feel free to delete it or close it!
If you do a select like this, all rows in one resultset are joined to all rows in the other resultset (Cartesian Product).
So you get a list of all rows of the first table with the first row of the second table, Then all entries for the second row and so on. The order may be an implementation detail. Not sure if it is defined that the first order is by the first table, it might be different across implementations.
If you join three tables (or more), then the same happens with all rows of all tables. This, of course, is not only for tables, but for any result set from joins.
The result will be a cartisian product
take a look at this example
SQL Example
You can see there are two tables one has 5 records and the other has 4 and the result is 20 records. Means 5 * 4 = 20 instead of 5 + 4 = 9 as you are assuming.
Table1
| IDX | VAL |
---------------
| 1 | 1val1 |
| 1 | 1val2 |
| 2 | 2val1 |
| 2 | 2val2 |
| 2 | 2val3 |
Table2
| ID | POINTS |
---------------
| 1 | 2 |
| 2 | 10 |
| 3 | 21 |
| 4 | 29 |
Result of below query
SELECT * FROM Table1 , Table2
| IDX | VAL | ID | POINTS |
-----------------------------
| 1 | 1val1 | 1 | 2 |
| 1 | 1val1 | 2 | 10 |
| 1 | 1val1 | 3 | 21 |
| 1 | 1val1 | 4 | 29 |
| 1 | 1val2 | 1 | 2 |
| 1 | 1val2 | 2 | 10 |
| 1 | 1val2 | 3 | 21 |
| 1 | 1val2 | 4 | 29 |
| 2 | 2val1 | 1 | 2 |
| 2 | 2val1 | 2 | 10 |
| 2 | 2val1 | 3 | 21 |
| 2 | 2val1 | 4 | 29 |
| 2 | 2val2 | 1 | 2 |
| 2 | 2val2 | 2 | 10 |
| 2 | 2val2 | 3 | 21 |
| 2 | 2val2 | 4 | 29 |
| 2 | 2val3 | 1 | 2 |
| 2 | 2val3 | 2 | 10 |
| 2 | 2val3 | 3 | 21 |
| 2 | 2val3 | 4 | 29 |
I think you are confusing yourself by running an example with two tables that have identical fields. You are referring to a Union, which will combine the values of 1 table with another, and using your example this would give you 3 + 4 = 7 results.
The comma separated FROM statement is doing JOIN, which will go through all values in Table X and pair them with all the values of Table Y. This would result in Size of X * Size of Y results, and using your example this would be 3 * 4 = 12.