thank you in advance for your help.
I have an SQL table which looks like this.
date and serial number are a composite key together. Meaning there cannot be a tuple with the same date and the same serial_number. Now, I want to get the most recent date (transaction) per serial_number. How can I do this? This is what i tried but it gives me some duplicates.
SELECT DATE_FORMAT(`t1`.date,'%m-%d-%y') as date , `t1`.serial_number
FROM table1 `t1`
WHERE date IN (SELECT MAX(date) FROM table1 GROUP BY serial_number)
order by `t1`.date desc, `t1`.serial_number asc;
You need correlated subquery not only subquery :
SELECT DATE_FORMAT(t.date,'%m-%d-%y') as date, t.serial_number
FROM table1 t
WHERE t.date = (SELECT MAX(t1.date)
FROM table1 t1
WHERE t1.serial_number = t.serial_number
)
order by t.date desc, t.serial_number asc;
You could use a inner join on max_date for serial_number
> SELECT DATE_FORMAT(`t1`.date,'%m-%d-%y') as date
> , `t1`.serial_number FROM `table` `t1` INNER JOIN (
> SELECT serial_number, MAX( DATE_FORMAT(`T`.date,'%m-%d-%y')) max_date
> FROM `table` T
> GROUP BY serial_number ) T ON T.serial_number = `t1`.serial_number AND T.max_date = DATE_FORMAT(`t1`.date,'%m-%d-%y')
>
> order by `t1`.date desc, `t1`.serial_number asc;
Related
I Have this table named Transaction :
And i am trying to make a SQL query on acess that would find, for each product, the most recent date associated. The results should look like this : (i want to see the quantity as well as the date and product name)
Thanks
Consider filtering with a subquery:
select t.*
from transactions t
where t.date = (
select max(t1.date)
from transactions t1
where t1.product = t.product
)
Try with row_number if you are using MySQL 8.0
select
id,
quantity,
date
from
(
select
id,
quantity,
date,
row_number() over (partition by product order by date desc) as rn
from transaction
) subq
where rn = 1
You have to group the rows by product while selecting the latest (max) date:
select t.product, t.quantity, t.date
from transaction t
inner join (
select product, max(date) as MaxDate
from transaction
group by product
) tm on t.product = tm.product and t.date = tm.MaxDate
How do i get the same result but without using "limit" in mysql?
SELECT user_id
FROM user_interest
GROUP BY user_id
HAVING COUNT(user_id)
ORDER BY (COUNT(user_id)) DESC
LIMIT 2
Here's some suggestion if you don't want to use limit
select t2.user_id
from (
select row_number() over (order by t1.ct desc) as rn, t1.userid
from (
SELECT user_id, COUNT(user_id) as ct
FROM user_interest
GROUP BY user_id
HAVING COUNT(user_id)
)t1
) as t2 where t2.rn < 3
Let's say I have a table MyTable with the columns Id, NumericValue and UTCTimestamp. I'm trying to group the results of my table MyTable by the hour of their timestamp and return the maximum NumericValuefor each group with its associated timestamp as well as the minimum NumericValue for each group with its associated timestamp value.
For now, I'm able to achieve the first part of my problem with the following query:
SELECT
HOUR(t.UTCTimestamp) AS `Hour`,
t.NumericValue AS MaximumValue,
t.UTCTimestamp AS MaximumValueTime
FROM MyTable t
INNER JOIN (
SELECT HOUR(t2.UTCTimestamp) AS `Hour`, MAX(t2.NumericValue) AS NumericValue
FROM MyTable t2
GROUP BY HOUR(t2.UTCTimestamp)
) maxNumericValue ON HOUR(t.UTCTimestamp) = maxNumericValue.`Hour` AND t.NumericValue = maxNumericValue.NumericValue
GROUP BY HOUR(t.UTCTimestamp);
Which was inspired by this answer.
Here's an MVCE.
How could I also show the minimum value for each group as well as the timestamp associated to it?
Starting from MySQL 8.0 you could use ROW_NUMBER:
WITH cte AS (
SELECT *,ROW_NUMBER() OVER(PARTITION BY HOUR(UTCTimestamp)
ORDER BY UTCTimestamp ASC) AS rn
,ROW_NUMBER() OVER(PARTITION BY HOUR(UTCTimestamp)
ORDER BY UTCTimestamp DESC) AS rn2
FROM MyTable
)
SELECT HOUR(c1.UTCTimestamp),
c1.ID, c1.NumericValue, c1.UTCTimestamp, -- min row
c2.ID, c2.NumericValue, c2.UTCTimestamp -- max row
FROM cte c1
JOIN cte c2
ON HOUR(c1.UTCTimestamp) = HOUR(c2.UTCTimestamp)
AND c1.rn=1
AND c2.rn2=1
ORDER BY HOUR(c1.UTCTimestamp) ASC;
DBFiddle Demo
You can join to MyTable twice (and only use one aggregating subquery)
SELECT bounds.`Hour`
, minT.NumericValue AS MinValue
, minT.UTCTimestamp AS MinTime
, maxT.NumericValue AS MaximumValue
, maxT.UTCTimestamp AS MaximumValueTime
FROM (
SELECT HOUR(t2.UTCTimestamp) AS `Hour`
, MAX(t2.NumericValue) AS maxValue
, MIN(t2.NumericValue) AS minValue
FROM MyTable t2
GROUP BY HOUR(t2.UTCTimestamp)
) bounds
LEFT JOIN MyTable minT ON bounds.`Hour` = HOUR(minT.UTCTimestamp)
AND bounds.minValue = minT.NumericValue
LEFT JOIN MyTable maxT ON bounds.`Hour` = HOUR(maxT.UTCTimestamp)
AND bounds.maxValue = maxT.NumericValue
;
Apply the same technique but with minimum:
select a.*, b.MinimumValueTime from (
SELECT
HOUR(t.UTCTimestamp) AS `Hour`,
t.NumericValue AS MaximumValue,
t.UTCTimestamp AS MaximumValueTime
FROM MyTable t
INNER JOIN (
SELECT HOUR(t2.UTCTimestamp) AS `Hour`, MAX(t2.NumericValue) AS NumericValue
FROM MyTable t2
GROUP BY HOUR(t2.UTCTimestamp)
) maxNumericValue ON HOUR(t.UTCTimestamp) = maxNumericValue.`Hour` AND t.NumericValue = maxNumericValue.NumericValue
GROUP BY HOUR(t.UTCTimestamp))a
join
(
SELECT
HOUR(t.UTCTimestamp) AS `Hour`,
t.NumericValue AS MinimumValue,
t.UTCTimestamp AS MinimumValueTime
FROM MyTable t
INNER JOIN (
SELECT HOUR(t2.UTCTimestamp) AS `Hour`, MIN(t2.NumericValue) AS NumericValue
FROM MyTable t2
GROUP BY HOUR(t2.UTCTimestamp)
) minNumericValue ON HOUR(t.UTCTimestamp) = minNumericValue.`Hour` AND t.NumericValue = minNumericValue.NumericValue
GROUP BY HOUR(t.UTCTimestamp))b on a.hour=b.hour
I want to return only the newest rows that have a different video_id.
I have been having trouble getting this to work no matter which way I try it... I have done this before and it isn't that difficult but for some reason my particular query will not work.
My table/results:
I have been trying this:
SELECT * FROM user_video_history
WHERE `user_id` = $db_safe_user_id
GROUP BY video_id
ORDER BY `date` DESC LIMIT 3
I have also tried this:
SELECT *
FROM (SELECT *
FROM user_video_history
WHERE `user_id` = $db_safe_user_id
ORDER BY `date` DESC)
GROUP BY `video_id`
ORDER BY `date` DESC
You cannot use select * when you GROUP BY
Try this...
SELECT
A . *
FROM
user_video_history A,
(SELECT
video_id, max(date) maxdate
FROM
user_video_history
WHERE
`user_id` = $db_safe_user_id
GROUP BY video_id) B
where
A.video_id = B.video_id
and A.date = B.maxdate
order by A.date DESC
limit 3
Try this:
SELECT
t1 . *
FROM
user_video_history t1
JOIN
(SELECT
id, MAX(`date`) `date`
FROM
user_video_history
GROUP BY user_id) t2 ON t1.user_id = t2.user_id
AND t1.`date` = t2.`date`
LIMIT 3
I have a table with lots of fields in mysql
I need a query to return (in the same raw!) the top last 3 dates (dates can have large gaps between them)
ie:
2012/01/20
2012/01/18
2012/01/12
2012/01/10
2012/01/04
etc...
Any help will be appreciated
I must get them in the same row!
This is the query I am trying to use with no success:
SELECT a.id, a.thedate, b.id AS id1, b.thedate AS thedate1, c.id AS id2, c.thedate as thedate2
FROM mytable AS a INNER JOIN mytable AS b ON a.id = b.id INNER JOIN mytable AS c ON b.id=c.id
WHERE c.thedate = SELECT MAX(thedate)
EDIT :
SELECT group_concat(date) FROM (SELECT date FROM my_table ORDER BY date DESC LIMIT 3) AS temp
Corrected-
SELECT group_concat(date) FROM ( select date from table_name order by date desc limit 3) as a
SELECT GROUP_CONCAT(a.date )
FROM (
SELECT date
FROM my_table
ORDER BY date DESC
LIMIT 3
) AS a