Quantcast
Channel: User Schwern - Stack Overflow
Viewing all articles
Browse latest Browse all 581

Answer by Schwern for How to select a row group by a column regardless of difference in other columns

$
0
0

If you want to select a whole row which has a max of something, you need to use window functions to partition and order the rows, then select the first in each partition.

with ordered_users as (  select    *,    -- This will be 1 for the bill_id with the highest complete_date.    row_number() over (      partition by user_id      order by complete_date desc    ) as row_num  from tbl_user_complete)select user_id, bill_id, complete_datefrom ordered_userswhere row_num = 1;

Demonstration


Viewing all articles
Browse latest Browse all 581

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>