简体   繁体   中英

Latest date with SQL Query

Thanks in advance for assisting me. I have the following table:

 Name | LastUpdate  |   Amount  |
 ABC  |  2014-4-9   |    100    |
 ABC  |  2014-4-9   |  **101**  |
 ABC  |  2014-4-8   |     99    |
 DEF  |  2014-4-9   |  **200**  |
 DEF  |  2014-4-8   |    160    |
 GHI  |  2014-4-9   |   **50**  |
 GHI  |  2014-4-8   |     80    |

My queries doesn't seems to work to get the following result: The sum of Amount for the latest date and highest amount for all names.

Example answer for above is 351 (101+200+50).

I based this solution on the answer to

find maximum of set of columns for multiple rows in mysql query

SELECT SUM(Amount) AS Total
FROM table1 AS t
WHERE LastUpdate = (SELECT MAX(LastUpdate) 
                    FROM table1 
                    WHERE NAme = t.Name)
AND Amount = (SELECT MAX(Amount) 
              FROM table1
              WHERE Name = t.Name
              AND LastUpdate = t.LastUpdate)

DEMO

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM