MySql subqueries and max or group by? - mysql

I have this table:
ID STUDENT CLASS QUESTION ANSWER TIME
1 1 1 1 c 12:30
2 1 1 1 d 12:36
3 1 1 2 a 12:38
4 2 1 1 b 11:24
5 2 1 1 c 11:26
6 2 1 3 d 11:35
7 2 3 3 b 11:24
I'm trying to write a query that does this:
For each STUDENT in a specific CLASS select the most recent ANSWER for each QUESTION.
So, choosing class "1" would return:
ID STUDENT CLASS QUESTION ANSWER TIME
2 1 1 1 d 12:36
3 1 1 2 a 12:38
5 2 1 1 c 11:26
6 2 1 3 d 11:35
I've tried various combinations of subqueries, joins, and grouping, but nothing is working. Any ideas?

You can use a sub-query to get most recent ANSWER per QUESTION, Then use this as a derived table and join back to the original table:
SELECT m.*
FROM mytable AS m
INNER JOIN (
SELECT STUDENT, QUESTION, MAX(`TIME`) AS mTime
FROM mytable
WHERE CLASS = 1
GROUP BY STUDENT, QUESTION
) AS d ON m.STUDENT = d.STUDENT AND m.QUESTION = d.QUESTION AND m.`TIME` = d.mTime
WHERE m.CLASS = 1
Demo here

Related

query to get distinct data from child table

please provide query to getting my result.
i have two tables as follows.
price_band
id club_id name price
1 6 test 2.3
2 6 test1 3.3
price_band_seat
id price_band_id row seat block_id
1 1 a 1 1
2 1 a 2 1
3 1 b 1 2
4 2 b 2 2
and result that i want
Price block_id price_band_id row
2.3 1 1 a
2.3 2 1 b
3.3 2 2 b
query exclude that raw which block_id and price_band_id are same . in where clues you have to take club_id=6
Please try this.
SELECT
DISTINCT
A.Price,B.block_id,B.price_band_id,B.row
FROM
price_band A
INNER JOIN price_band_seat B
ON A.id = B.price_band_id
WHERE A.club_id = 6

MySQL filter out subset by group by

1A
a b c
1 1 6
1 1 7
2 1 8
2 2 2
2 2 9
B
a b c
1 1 7
2 2 9
I want to filter out a subset of A
a b c
1 1 6
2 2 2
I am intend to join two tables by group by column a, b
such that to select the value in column c is less than the c value in table B, which is the desired subset.
But don't know how to implement this.
Try this:
SELECT A.* FROM A INNER JOIN B
ON A.a=B.b AND A.c<B.c;
See MySQL Join Made Easy tutorial.

MySQL COUNT values fo each column

I’m trying to figure out how to count the values in more than one column.
It seem the first COUNT I do gives me the correct results but everything I’ve tried to get the second column count gives the wrong result.
For example, with the following two columns,
Q2 Q3
1 1
1 1
2 2
1 1
1 1
5 5
3 5
5 3
4 1
2 2
3 3
3 3
5 5
3 3
2 1
2 1
3 2
4 1
1 1
1 1
2 2
5 5
3 3
2 1
3 3
1 1
2 1
SELECT COUNT(Q2) AS QU2 FROM mytable GROUP BY Q2
QU2 = 7 7 7 2 4
gives me the count for Q2. 7 one’s, 7 two’s and so on...
However, the following gives me an unexpected result.
SELECT COUNT(Q2) AS QU2, COUNT(Q3) AS QU3 FROM mytable GROUP BY Q2, Q3
7 4 3 1 5 1 2 1 3
I think its something with the GROUP BY but I don’t know how to get around it to get the needed result.
So I'm tying to get the result of
QU2 = 7 7 7 2 4
QU3 = 13 4 6 4
Or
QU2 QU3
7 13
7 4
7 6
2 4
4
and so on for QU4 QU5 ... I would appreciate any help.
Thank you
I think that this will get you closest to what you want. You can replace the numbers table with any method that generates the numbers 1 to whatever the max value is in Q2 or Q3.
CREATE TABLE dbo.Numbers (num INT)
INSERT INTO dbo.Numbers (num) VALUES (1), (2), (3), (4), (5)
SELECT
N.num,
SUM(CASE WHEN MT.Q2 = N.num THEN 1 ELSE 0 END) AS QU2,
SUM(CASE WHEN MT.Q3 = N.num THEN 1 ELSE 0 END) AS QU3
FROM
dbo.Numbers N
CROSS JOIN dbo.My_Table MT
GROUP BY
N.num
Adding additional columns (for Q4, etc.) just means adding another SUM(CASE...)
How GROUP BY works?
Let's talk about your first query (I added the column Q2 in the SELECT clause to make its output more clear):
SELECT Q2, COUNT(*) AS QU2
FROM mytable
GROUP BY Q2
First, it gets all the rows matching the WHERE criteria, if a WHERE clause exists. Because your query doesn't have a WHERE clause, all the rows from the table are read.
On the next step the rows read on the previous step are grouped by the expression specified in the GROUP BY clause (let's assume it contains only one expression, as the query above does). Internally, grouping the rows requires sorting them first.
This is how the data is organized on this step. I added horizontal separators between the rows that go in each group to make everything clear:
Q2 Q3
-------
1 1
1 1
1 1
1 1
1 1
1 1
1 1
-------
2 1
2 1
2 1
2 1
2 2
2 2
2 2
-------
3 2
3 3
3 3
3 3
3 3
3 3
3 5
-------
4 1
4 1
-------
5 3
5 5
5 5
5 5
-------
On the next step, from each group it creates a single row that goes to the generated result set.
The query above clearly returns:
Q2 QU2
--------
1 7
2 7
3 7
4 2
5 4
What happens when the GROUP BY clause contains more than one expression?
Let's take your second query (again, I added some columns to show its behaviour):
SELECT Q2, Q3, COUNT(*) AS cnt
FROM mytable
GROUP BY Q2, Q3
It works similar with the previous query but, because the GROUP BY clause contains two expression, each group created for the values of Q2 is split in sub-groups based on the value of Q3. Assuming there is another expression (let' say, Q4) in the GROUP BY clause, each sub-group created for a pair (Q2, Q3) is further divided into sub-groups for all the values of Q4 and so on.
For your table, the groups and sub-groups are as follows:
Q2 Q3
=======
1 1
1 1
1 1
1 1
1 1
1 1
1 1
=======
2 1
2 1
2 1
2 1
---
2 2
2 2
2 2
=======
3 2
---
3 3
3 3
3 3
3 3
3 3
---
3 5
=======
4 1
4 1
=======
5 3
---
5 5
5 5
5 5
=======
I used double lines to separate the groups and smaller single lines to separate the subgroups inside each group.
The output of this query is:
Q2 Q3 cnt
------------
1 1 7
2 1 4
2 2 3
3 2 1
3 3 5
3 5 1
4 1 2
5 3 1
5 5 3
How to get the desired result?
It is not possible to get the result you want using a single query. Even more, the result sets you suggest doesn't make much sense.
You can combine two queries using UNION in order to get the data you need and additional information that helps you know where those numbers come from:
SELECT 'Q2' AS source, Q2 AS q, COUNT(Q2) AS cnt FROM mytable GROUP BY Q2
UNION
SELECT 'Q3' AS source, Q3 AS q, COUNT(Q3) AS cnt FROM mytable GROUP BY Q3
The output is:
source q cnt
----------------
Q2 1 7
Q2 2 7
Q2 3 7
Q2 4 2
Q2 5 4
Q3 1 13
Q3 2 4
Q3 3 6
Q3 5 4
Pretty clear, isn't it? The first 5 rows come from the query GROUP BY Q2 and their value in the column q tells what was the value of Q2 for each group (there are 7 occurrences of 1 in column Q2, 7 of 2, 7 of 3, 2 of 4 and so on). The last 4 rows tell the similar story about Q3 (13 rows have 1 in column Q3 and so on).
Remark
There is a difference between COUNT(*) and COUNT(Q2): COUNT(*) counts the rows from the group, COUNT(Q2) counts the not-NULL values in the column Q2. It doesn't care about duplicate, it only ignore the NULL values. If you want to count the distinct values then you have to add the DISTINCT keyword: COUNT(DISTINCT Q2).
I think you need to unpivot the data. In this case, that just means multiple group by connected by union all:
select 'q2' as which, q2, count(*) as cnt
from mytable
group by q2
union all
select 'q3' as which, q3, count(*) as cnt
from mytable
group by q3;
You can add as many more subqueries as you like.
Note: this puts the values in separate rows, rather than in separate columns.
I reckon Asaph is on the right track , however I would alter this slightly try select distinct count(Q2) as QU2, count(Q3) as QU3 from myTable;

MySQL Query 2 records at top followed by the rest

I'm stuck with the following problem.
I have a website with for example supermarket shopping items. People can search the website for items.
Now I want on the search result page at the top 2 items to be displayed that I have selected to be on offer. There can be lots more items on offer.
So for example, someone would search for shampoo, the query would display all the shampoo items in the database table but I want just 2 shampoo offer items at the top of the query. There could be 2 or more shampoo offers in the database table, then the other would just not be shown.
Example with names :
Table:
id name C D
----------------------------------
1 Jack 1 1
2 Joe 1 1
3 Dave 3 0
4 Sue 1 0
5 Mike 1 1
6 Steve 4 0
7 David 1 0
8 Susan 4 1
9 Marc 1 1
10 Ronald 4 1
11 Michael 4 1
EXAMPLE 1
Query :
WHERE C = 1 AND D = 1 (But only maximum of 2 'D' records, these 2 'D' records show at the top of the result)
Desired Query Result :
id name C D
----------------------------------
1 Jack 1 1
2 Joe 1 1
4 Sue 1 0
7 David 1 0
EXAMPLE 2
Query :
WHERE C = 4 AND D = 1 (But only maximum of 2 'D' records, these 2 'D' records show at the top of the result)
Desired Query Result :
id name C D
----------------------------------
8 Susan 4 1
10 Ronald 4 1
6 Steve 4 0
I hope this explains my goal what I'm trying to achieve.
Many thanks for any help or suggestions!
That's two queries that you can combine with UNION ALL:
select * from mytable where c = 1 and d = 1 limit 2
union all
select * from mytable where c = 1 and d = 0
order by d desc;
UPDATE: If you want to have the two rows chosen randomly, then order by RAND(). (Without an ORDER BY the rows are chosen arbitrarily, which means it's not guaranteed to get the same two rows picked again when re-running the query &dash; but it's quite likely.) As we need an ORDER BY for a partial query (the first query in the complete union-alled query), we must use parentheses, because otherwise only one ORDER BY would be allowed, namely for the complete query at the query's end.
(select * from mytable where c = 1 and d = 1 order by rand() limit 2)
union all
(select * from mytable where c = 1 and d = 0)
order by d desc;
SQL fiddle: http://sqlfiddle.com/#!9/ed53f6/3.

How to get the count of rows from a table in cakephp

I have two tables:
id category status
1 test 1
2 test1 1
3 test2 1
This is the group_master table.
groupid groupname groupmaster
1 yy 1
2 xx 1
3 yyyy 1
4 xrx 1
5 yy 2
6 xx 2
7 yyyy 2
8 xfgdrx 3
This is the membergroup table.
The group_master.id is same as in membergroup.groupmaster.That menas i want to get each row from first table and also want to get the count from second table
That means:
id category status Count
1 test 1 4
2 test1 1 3
3 test2 1 1
This is the result i want to get.
How can i do this in Cakephp with pagination ?
try this:
You need to JOIN both tables and do a GROUP BY
SELECT g.id,g.category,g.status,count(*) as Count
FROM group_master g
JOIN membergroup m
ON g.id=m.groupmaster
GROUP BY g.id,g.category,g.status
SQL Fiddle Demo