1

I am having some challenges spooling out some database records.

I need to get the rows with MAX value for a specific column and these records must fall between two timestamp values.

Here is the SQL query

SELECT id, MAX(amount), created 
FROM `product` 
where author = '1' AND (created BETWEEN '2018-02-03' AND '2018-02-08') 
GROUP BY id

I am able to get the records with MAX value in an accurate order using

SELECT id, MAX(amount), created FROM `product` where author = '1' GROUP BY id 

But the moment I include a clause to distinct records between my desired timestamp, I lose the accurate order in MAX

Would be really glad to get some help with this. Thanks

Barmar
  • 741,623
  • 53
  • 500
  • 612
Ous
  • 71
  • 1
  • 8

1 Answers1

0

GROUP BY then ORDER BY MAX(amount)

SELECT id, MAX(amount), created 
FROM `product` 
where author = '1' AND (created BETWEEN '2018-02-03' AND '2018-02-08') 
GROUP BY id
ORDER BY 2 DESC
D-Shih
  • 44,943
  • 6
  • 31
  • 51
  • Thanks a bunch.. The `ORDER BY 1 DESC` was the game changer and I really like the approach and It just made me understood some basic things regarding record ordering in mysql... It simply changed from the default `ORDER BY id ASC` to yours... Thanks a million... – Ous Feb 10 '18 at 02:30