Getting max post_year group by email with num of years

最后都变了- 提交于 2019-12-24 07:56:02

问题


Given the table below

Email  Post_Year  country
====== ========== =======
a@a.com  2006       US
a@a.com  2007       US
a@a.com  2008       HK1
a@a.com  2008       HK
b@b.com  2009       CN
b@b.com  2010       SW

I want to have

  1. all columns with max Post_year group by email, if there are multiple max Post_year, just choose one

  2. as well as the num_of_yrs which is max(post_year)-min(post_year) of that particular email.

Email  Post_Year  country  Num_Of_Yrs
====== ========== ======= =============
a@a.com  2008      HK       2 [which is 2008-2006]
b@b.com  2010      SW       1 [which is 2010-2009]

How to achieve the purpose?


回答1:


Edited: With your edited intention, you could simply use:

SELECT 
    email, 
    MAX(post_year) post_year,
    MAX(country) country, 
    MAX(post_year) - MIN(post_year) num_of_yrs
FROM 
    table_name
GROUP BY 
    email;

You could use this:

WITH tmp AS
(
    SELECT 
        email, 
        MAX(post_year) max_post_year,
        MIN(post_year) min_post_year
    FROM 
        table_name
    GROUP BY 
        email
)
SELECT 
    t.email,
    t.post_year,
    t.country,
    tmp.max_post_year - tmp.min_post_year num_of_yrs
FROM 
    table_name t
INNER JOIN 
    tmp
ON t.email = tmp.email
    AND t.post_year = tmp.max_post_year;



回答2:


You can use window function row_number to get one row with max year per email and window functions max and min to get max difference for each email.

Try this:

select *
from (
    select t.*,
        row_number() over (partition by email 
                            order by post_year desc) rn,
        max(post_year) over (partition by email) -
        min(post_year) over (partition by email) num_of_yrs
    from your_table t
    ) t where rn = 1;


来源:https://stackoverflow.com/questions/42325221/getting-max-post-year-group-by-email-with-num-of-years

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