Fetch row which has the maximum value for a column + Minimum value of the same column in the same row

℡╲_俬逩灬. 提交于 2019-12-12 05:28:24

问题


I checked the following questions:

GROUP BY with MAX(DATE)

Fetch the row which has the Max value for a column

Columns : account_number, app_guid, time_event_published(epoch time).

I want the latest row of each app_guid for a given account_number PLUS oldest time_event_published of the same account_number for each app_guid in another column of the latest row.

SELECT id, account_number, app_guid, time_event_published , <oldest_time_event_published_for_2152331553409959696> FROM ( 
  SELECT id, account_number, app_guid, time_event_published,
    RANK() OVER (PARTITION BY app_guid ORDER BY time_event_published DESC) dest_rank
    FROM event where account_number=2152331553409959696
  ) where dest_rank = 1;

I am only able to think of another DB hit with same query with ASC. Is there any other way and how to approach this requirement?

DB Entries:
2152331553409959696, TEST-ONE-APP_GUID, 25-JAN
2152331553409959696, TEST-ONE-APP_GUID, 1-JAN

2152331553409959696, TEST-TWO-APP_GUID, 25-FEB
2152331553409959696, TEST-TWO-APP_GUID, 1-FEB

Required Result:
2152331553409959696, TEST-ONE-APP_GUID, 25-JAN, 1-JAN
2152331553409959696, TEST-TWO-APP_GUID, 25-FEB, 1-FEB

回答1:


If I understood your question correctly then I think the below SQL will do what you require:

SELECT id, account_number, app_guid, time_event_published , oldest_time_event_published FROM ( SELECT id, account_number, app_guid, time_event_published, RANK() OVER (PARTITION BY app_guid ORDER BY time_event_published DESC) dest_rank, MIN (time_event_published) OVER (PARTITION BY app_guid) oldest_time_event_published FROM event where account_number=2152331553409959696 ) where dest_rank = 1;

Let me know how it works with your data and let us know.

I haven't tested with sample data but I am pretty confident it will work for you!

Ted.




回答2:


This should work, it's not very attractive though, whether it's better than hitting the table twice depends on performance and readability.

SELECT  MAX(CASE WHEN dest_rank = 1 THEN id
                 ELSE NULL
             END
           ) AS id,
        MAX(CASE WHEN dest_rank = 1 THEN account_number
                 ELSE NULL
             END
           ) AS account_number,
        app_guid,
        MAX(CASE WHEN dest_rank = 1 THEN time_event_published
                 ELSE NULL
             END
           ) AS time_event_published,
        MAX(CASE WHEN dest_rank_asc = 1 THEN time_event_published
                 ELSE NULL
             END
           ) AS earliest_time_event_published
  FROM (SELECT id, 
               account_number, 
               app_guid, 
               time_event_published,
               RANK() OVER 
                 ( PARTITION BY app_guid 
                        ORDER BY time_event_published DESC
                 ) dest_rank,
               RANK() OVER 
                 ( PARTITION BY app_guid 
                       ORDER BY time_event_published ASC
                 ) dest_rank_asc,
          FROM event 
         WHERE account_number=2152331553409959696
        ) 
 GROUP
    BY app_guid;


来源:https://stackoverflow.com/questions/46062964/fetch-row-which-has-the-maximum-value-for-a-column-minimum-value-of-the-same-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!