I have a table full of magazines, and need to extract the latest unique issue of each magazine.
Ive tried
SELECT DISTINCT
magazine
FROM
product p
INNER JOIN
(SELECT
title, MAX(onSale) AS Latest
FROM
product
GROUP BY magazine) groupedp
Which returns the distinct magazines , but not the rest of the data I require.
UPDATE:
schema
-id----onsale----magazine
1 1/12/12 Fishing Mag
2 1/11/12 Fishing Mag
3 12/03/11 Pencil Sharpening Monthly
4 1/02/10 Pencil Sharpening Monthly
5 16/04/09 Homes in the Sky
So the result I would like returned would be:
-id----onsale----magazine
1 1/12/12 Fishing Mag
3 12/03/11 Pencil Sharpening Monthly
5 16/04/09 Homes in the Sky
How to implement MAX(distinct…) in MySQL and what is the difference without using DISTINCT? select max( yourColumnName) from yourTableName; NOTE − Both the above queries give the same result with or without a DISTINCT keyword. MySQL internally converts MAX(yourColumnName) to DISTINCT keyword.
The MySQL Solution If you're working with MySQL, you can combine MAX() with the GREATEST() function to get the biggest value from two or more fields. Here's the syntax for GREATEST: GREATEST(value1,value2,...) Given two or more arguments, it returns the largest (maximum-valued) argument.
Solution. Use select max(num) to select the biggest number. To select the biggest number that only appears once, use having count(num) = 1 as a condition.
SQL MIN() and MAX() Functions The MIN() function returns the smallest value of the selected column. The MAX() function returns the largest value of the selected column.
SELECT
p.*
FROM
product p
INNER JOIN
( SELECT
magazine, MAX(onSale) AS latest
FROM
product
GROUP BY
magazine
) AS groupedp
ON groupedp.magazine = p.magazine
AND groupedp.latest = p.onSale ;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With