I am trying to select from a table like this below and limit it by the latest date.
record_date
Name
Age
2021-05-19
John
21
2021-05-19
Smith
25
2021-05-19
Mary
20
2021-05-20
Ann
26
2021-05-20
Jane
23
I tried doing
SELECT * FROM table WHERE DATE(record_date) BETWEEN MAX(DATE(record_date)) AND MAX(DATE(record_date))
The above code does not work, but this is what I am trying to acheive below:
record_date
Name
Age
2021-05-20
Ann
26
2021-05-20
Jane
23
I don't want to hard-code the date into the statement, because records come in everyday, so it has to be able to select the latest date and limit only that. Thanks!
Something like:
select * from table where record_date = (select max(record_date) from table)
One more query using JOIN
select t.*
from t
join (select max(record_date) max_date from t) max_date_t on record_date = max_date;
MySQL fiddle
Related
I have a table where each quiz ID is repeated multiple times. there is a date in front of each quiz id in each row. I want to select entire row for each quiz ID where date is latest with user. The date format is mm/dd/YYYY.
Sample -
USER_ID Quiz_id Name Date Marks .. .. ..
1 2 poly 4/3/2020 27
1 2 poly 4/3/2019 98
1 4 moro 4/3/2020 09
2 5 cat 4/12/2015 87
2 4 moro 4/3/2009 56
2 6 PP 4/3/2011 76
3 2 poly 4/3/2020 12
3 2 poly 5/3/2020 09
3 7 dog 4/3/2011 23
I want result look like this:Result
USER_ID Quiz_id Name Date Marks .. .. ..
1 2 poly 4/3/2020 27
1 4 moro 4/3/2020 09
2 5 cat 4/12/2015 87
2 4 moro 4/3/2009 56
2 6 PP 4/3/2011 76
3 2 poly 5/3/2020 09
3 7 dog 4/3/2011 23
You can use rank function to get the desired result:
Demo
SELECT A.* FROM (
SELECT A.*, RANK() OVER(PARTITION BY USER_ID,QUIZ_ID, NAME ORDER BY DATE DESC) RN FROM
Table1 A ORDER BY USER_ID) A WHERE RN = 1 ORDER BY USER_ID, QUIZ_ID;
I don't have MySQL installed so you will need to test and report back. The general idea is to identify the row of interest using max and a group by (table t). As the Date column appears to be text column (MySQL uses the format YYYY-MM-DD for dates) you will need to convert it to a date with str_to_date() so you can use the max() aggregate function. Finally, join with the original table (here table t2 to do the date conversion), as only the aggregate column(s) and columns named in the group by are well defined (in table t1), i.e.:
select USER_ID, Quiz_id, Date, Marks from (
select USER_ID, Quiz_id, max(str_to_date(Date, '%m/%d/%Y')) as Date2 from quiz group by 1, 2
) as t natural join (
select *, str_to_date(Date, '%m/%d/%Y') Date2 from Quiz
) as t2;
I don't recall off-hand but Date might be reserved word, in which case you will need to quote the column name, or ideally rename said column to use a better name.
Also, the original table is not in 3rd normal form as Quiz_id depends on Name. Quiz_id, as implied, should be a foreign key to a lookup table that holds the Name.
Here is the data set:
Person Status Date
Eric 1 1/1/2015
Eric 2 2/1/2015
Eric 3 3/1/2015
John 1 3/1/2015
John 2 2/1/2015
John 1 1/1/2015
I'd like to get the most recent date, and its correlated status, grouped by Person. I tried using a subquery to first identify the most recent date:
SELECT MAX(Date), Person FROM tbl1 GROUP BY Person
And then joining that back into the original table, so that by person I know which date is the most recent. But I'm struggling how to identify the most recent status. I just don't see the appropriate aggregator. Thanks.
select tbl1.*
from tbl1
join
(
SELECT Person, MAX(Date) as m_date
FROM tbl1
GROUP BY Person
) tmp on tbl1.Person = tmp.Person
and tbl1.date = tmp.m_date
I have a table that logs every time a user completes a survey. It looks a bit like this:
surveyID author timestamp
-----------------------------------------------
1 person1 1461840669000
2 person2 1461840670000
3 person1 1461840680000
I'm trying to run a query that shows me the top surveyor every day (i.e. the person that does the highest number of surveys per day) since April 1st.
So far I've tried this:
SELECT author,
COUNT (DISTINCT surveyid) AS num_surveys,
STRFTIME_UTC_USEC(creation_time*1000, "%Y-%m-%d") AS date,
FROM myTable
WHERE creation_time > 1459468800000 //since April 1st
GROUP BY date, author
ORDER BY 3 DESC,2 DESC;
Which gives me this result:
author num_surveys date
------------------------------------
user1 116 2016-04-27
user2 109 2016-04-27
user3 99 2016-04-27
user3 102 2016-04-28
user1 98 2016-04-28
user2 97 2016-04-28
However, I would really just like the top record from each day:
author num_surveys date
------------------------------------
user1 116 2016-04-27
user3 102 2016-04-28 etc...
I've tried MAX() and TOP() in various places but none of them have worked so far hence the above example of my query that gets me closest to what I want... Any suggestions would be much appreciated. I'm very new to SQL!
EDIT
Thanks for the suggestions to far. Have managed to get it to work with:
DEFINE INLINE TABLE A
SELECT author,
COUNT (DISTINCT featureid) AS num_surveys,
STRFTIME_UTC_USEC(creation_time*1000, "%Y-%m-%d") AS date,
FROM placesense.surveys
WHERE creation_time > 1459468800000
GROUP BY date, author
ORDER BY 3 DESC,2 DESC;
SELECT
MAX(num_surveys),
date
FROM A AS B
WHERE date = B.date
GROUP BY date
Any other more efficient suggestions welcome though.
A pretty simple way uses a correlated subquery:
select t.*
from t
where t.num_surveys = (select max(t2.num_surveys) from t t2 where t2.date = t.date);
Note: this will return duplicates for a date in the case of ties.
SELECT MAX( surveyid) AS m_surveys,
STRFTIME_UTC_USEC(creation_time*1000, "%Y-%m-%d") AS date,
FROM myTable
WHERE creation_time > 1459468800000 //since April 1st
GROUP BY date, author
ORDER BY 3 DESC,2 DESC;
I'm stuck on this query. I need to do a group by date, card_id and only show the highest hits. I have this data:
date card_name card_id hits
29/02/2016 Paul Stanley 1345 12
29/02/2016 Phil Anselmo 1347 16
25/02/2016 Dave Mustaine 1349 10
25/02/2016 Ozzy 1351 17
23/02/2016 Jhonny Cash 1353 13
23/02/2016 Elvis 1355 15
20/02/2016 James Hethfield 1357 9
20/02/2016 Max Cavalera 1359 12
My query at the moment
SELECT DATE(card.create_date) `day`, `name`,card_model_id, count(1) hits
FROM card
Join card_model ON card.card_model_id = card_model.id
WHERE DATE(card.create_date) >= DATE(DATE_SUB(NOW(), INTERVAL 1 MONTH)) AND card_model.preview = 0
GROUP BY `day`, card_model_id
;
I want to group by date, card_id and filter the higher hits result showing only one row per date. As if I run a max(hits) with group by but I won't work
Like:
date card_name card_id hits
29/02/2016 Phil Anselmo 1347 16
25/02/2016 Ozzy 1351 17
23/02/2016 Elvis 1355 15
20/02/2016 Max Cavalera 1359 12
Any light on that will be appreciated. Thanks for reading.
Here is one way to do this. Based on your sample data (not the query):
select s.*
from sample s
where s.hits = (select max(s2.hits)
from sample s2
where date(s2.date) = date(s.date)
);
Your attempted query seems to have no relationship to the sample data, so it is unclear how to incorporate those tables (the attempted query has different columns and two tables).
I have an SQL selection which return the following:
Name Code Qty
Janet 10 6
Janet 11 9
Janet 09 8
Jones 12 7
Jones 11 8
James 09 5
James 10 4
I want this selection to get sorted based on the qty for all the three people : order the people by their maximum quantity, and then order by quantity.
The output should look like this:
Janet 11 9
Janet 09 8
Janet 10 6
Jones 11 8
Jones 12 7
James 09 5
James 10 4
Any way to achieve this?
This is a subtle problem. It looks like you want to sort the names by the maximum of qty. This requires a join and aggregation to get the maximum qty for each name:
select t.*
from table t join
(select name, max(qty) as maxq
from table t
group by name
) tt
order by tt.maxq desc, tt.name, t.qty desc;
Try this:
SELECT * FROM `names` ORDER BY name ASC, qty DESC
SELECT Name, Code, Qty
FROM names AS main JOIN
(SELECT Name, MAX(Qty) AS max_qty
FROM names
GROUP BY Name) AS max_names USING (Name)
ORDER BY max_names.max_qty DESC, names.Qty DESC
The virtual table max_names contains the maximal Qty for each Name:
Janet 9
Jones 8
James 5
Then you join it to the original table and sort according to this max_qty.
If you want to sort according to the total quantity per name, just replace MAX with SUM:
SELECT Name, Code, Qty
FROM names AS main JOIN
(SELECT Name, SUM(Qty) AS sum_qty
FROM names
GROUP BY Name) AS sum_names USING (Name)
ORDER BY sum_names.sum_qty DESC, names.Qty DESC
The sum_names table will contain:
Janet 23
Jones 15
James 9
You can specify more than one sorting condition:
SELECT * from names order by name, qty desc
Above query will sort by name and if names are equal then will sort by qty
If you want to select only higher qty for every user then use this query:
SELECT name, MAX(qty) FROM names GROUP BY name order by MAX(qty);