Wednesday, 15 June 2011

MySQL MAX() function returns wrong associated value -


I have tried various questions and have read different answers on this and I think what I actually see I am here. Advice is appreciated

I have a table with items, dates and quantity for each item, I need the latest date and associated volume. The table looks like this:

  Item date volume 1234 2014-12-22 300 1234 2015-02-13 500  

To run this query After: <

I get this:

  Item date volume 1234 2015-02-13 300  

qty to 500 , I've tried many different solutions and many more. First of all give all records with NULL in date . Second, I seem to work, as long as I have any date for sUM (qty) ( qty for items ) Code>). Since this solution is descending date , and does not actually cut any data, this scenario does not help.

Why does not my original query work? Any other ideas out there?

Thank you!

This does what you need to do to get you the join : Select

  T. Connect the table to T (select the item, Max (max) as the item from table T group to maxd) tmax t.item = tmax.item and t.date = tmax.maxd;  

Your query should fail because the qty group is not in it. MySQL allows it, but any other database will return an error.


No comments:

Post a Comment