hive中具有多个维度的滚动7天总和

amrnrhlw  于 2021-06-26  发布在  Hive
关注(0)|答案(1)|浏览(615)

我有如下数据,并希望得到(1)今天的用户数量和(2)过去7天的用户数量的维度如下。我的查询没有返回我想要的结果。有人能修复我的配置单元sql代码吗?
e、 g.)今天=2018年8月9日
过去7天=2018年8月2日-2018年8月8日
维度:日期、网站、国家/地区id、产品id
指标:用户

SELECT 
             date, 
             website,
             country_id,
             product_id,
             SUM(user) AS today_users,
             SUM(user) OVER (
           PARTITION BY website, country_id, product_id 
           ORDER BY date ASC ROWS BETWEEN 8 PRECEDING AND 1 PRECEDING) as past_7D_users
           FROM  Table
    GROUP BY 
    date, website, country_id, product_id;

原始数据:

>t_date  |website |country_id|product_id|users
    >8/1/2018   Whatev.com  2     EFG        12 
    >8/2/2018   Whatev.com  2     EFG        23
    >8/3/2018   Whatev.com  2     EFG        34 
    >8/4/2018   Whatev.com  2     EFG        13
    >8/5/2018   Whatev.com  2     EFG        47 
    >8/6/2018   Whatev.com  2     EFG        39
    >8/7/2018   Whatev.com  2     EFG        17 
    >8/8/2018   Whatev.com  2     EFG        34
    >8/1/2018   Google.com  1     ABC        10 
    >8/2/2018   Google.com  1     ABC        20
    >8/3/2018   Google.com  1     ABC        30 
    >8/4/2018   Google.com  1     ABC        14
    >8/5/2018   Google.com  1     ABC        40 
    >8/6/2018   Google.com  1     ABC        35
    >8/7/2018   Google.com  1     ABC        15 
    >8/8/2018   Google.com  1     ABC        32

预期结果:

date      | website  |country_id|product_id|today_users|past_7D_users|
2018-08-08|Google.com|   1      | ABC      | 32        |164          |
2018-08-09|Whatev.com|   2      | EFG      | 34        |185          |
ebdffaop

ebdffaop1#

好 啊。。自我回答我的问题很奇怪,但我找到了解决办法。如果有人知道一个更好的,简化的查询请分享。

SELECT x.date
,x.website
,x.country_id
,x.product_id
,x.today_users
,SUM(x.today_users) OVER (PARTITION BY x.website, x.country_id, x.product_id
                          ORDER BY x.date ASC ROWS BETWEEN 7 PRECEDING AND 1 PRECEDING) AS Past_7D_Users

FROM (SELECT 
             date, 
             website,
             country_id,
             product_id,
             SUM(user) AS today_users

    FROM  Table
    GROUP BY 
    date, website, country_id, product_id
)x
GROUP BY x.date
,x.website
,x.country_id
,x.product_id
,x.today_users
;

相关问题