简体   繁体   English

如何根据最新日期从 group by 查询中获取最高价格

[英]How to Get Max Price Based on Latest Date From group by query

Example Records :示例记录:

|name  |price |source|lastest_update|
|name A| 20.00|att   |04/10/2019 00:00:00|
|name A| 30.00|att   |04/11/2019 02:00:00|
|name A| 50.00|sprint|04/10/2019 01:00:00|
|name A| 40.00|sprint|04/11/2019 21:00:00|

Basically if we're using group by "group by name" the price that we'll get is the first one of the records, it's $20, but i want to get the max price based on lastest_update (date).基本上,如果我们使用“按名称分组”分组,我们将获得的价格是第一个记录,它是 20 美元,但我想根据 lastest_update(日期)获得最高价格。 So the results will be :所以结果将是:

|name  |att_price|sprint_price|
|name A|  30.00  |  40.00     |

My query我的查询

SELECT 
MAX(WHEN source = 'att' THEN price ELSE 0 END) as att_price,
MAX(WHEN source = 'sprint' THEN price ELSE 0 END) as sprint_price
FROM table GROUP BY name;

Thank you very much.非常感谢。

I suppose you can use a simple correlated query with LIMIT : 我想您可以将一个简单的相关查询与LIMIT

SELECT name
     , (SELECT price FROM t AS x WHERE name = t.name AND source = 'att'    ORDER BY lastest_update DESC LIMIT 1) AS att_price
     , (SELECT price FROM t AS x WHERE name = t.name AND source = 'sprint' ORDER BY lastest_update DESC LIMIT 1) AS sprint_price
FROM t
GROUP BY name

Or perhaps a double GROUP BY : 或者是双重GROUP BY

SELECT t.name
     , MAX(CASE WHEN t.lastest_update = a.att_date    THEN price END) AS att_price
     , MAX(CASE WHEN t.lastest_update = a.sprint_date THEN price END) AS sprint_price
FROM t
JOIN (
    SELECT name
         , MAX(CASE WHEN source = 'att'    THEN lastest_update END) AS att_date
         , MAX(CASE WHEN source = 'sprint' THEN lastest_update END) AS sprint_date
    FROM t
    GROUP BY name
) AS a ON t.name = a.name
GROUP BY t.name

Extract date from your timestamp column latest_update and apply max() to get the latest date. 从时间戳列latest_update提取date ,然后应用max()获取最新日期。 Now, you can just add a where condition to filter rows only having this max date. 现在,您只需添加一个where条件即可过滤仅具有此最大日期的行。

select name,
MAX(case WHEN source = 'att' THEN price ELSE 0 END) as att_price,
MAX(case WHEN source = 'sprint' THEN price ELSE 0 END) as sprint_price
FROM test 
where date(latest_update) = (select max(date(latest_update)) from test)
GROUP BY name;

Demo: https://www.db-fiddle.com/f/43Uy7ocCKQRqJSaYHGcyRq/0 演示: https //www.db-fiddle.com/f/43Uy7ocCKQRqJSaYHGcyRq/0

Update: 更新:

Since you need group by for each source according to individual source latest_update column, you can use the below SQL: 由于您需要根据各个源的latest_update列对每个source进行latest_update ,因此可以使用以下SQL:

select t1.name,
    max(
        case 
            when t1.source = 'att' then t1.price
            else 0
        end
      ) as att_price,
     max(
        case 
            when t1.source = 'sprint' then t1.price
            else 0
        end
      ) as sprint_price
from test t1
      inner join (
        select name,source,max(latest_update) as latest_update
        from test
        group by name,source) t2
      on t1.name = t2.name and t1.source = t2.source
      and t1.latest_update  = t2.latest_update
group by t1.name;

Demo: https://www.db-fiddle.com/f/qwBxizWooVG7AMwqKjewP/0 演示: https : //www.db-fiddle.com/f/qwBxizWooVG7AMwqKjewP/0

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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