gpt4 book ai didi

sql - Bigquery 滚动月份数据

转载 作者:行者123 更新时间:2023-12-02 02:48:54 25 4
gpt4 key购买 nike

我正在尝试实现这样的输出。用户在 3 个月内访问特定页面的次数。页面如主页、计数页面、购物车页面等。

我的 table

MMDDYY  Pagevisted  Username    No. of time Month
1/1/2019 Homepage A 1 January
2/21/2019 AccountPage A 1 February
2/25/2019 AccountPage B 5 February
3/1/2019 Homepage A 3 March
4/2/2019 cartpage B 2 April
5/2/2019 AccountPage A 1 May
6/2/2019 Submisison C 1 June
5/5/2019 Homepage D 2 May
5/2/2019 Articles E 2 May
7/25/2019 cartpage E 2 July
8/12/2019 Articles A 1 August
9/23/2019 Articles A 6 September

请您帮我查询以基于滚动的方法获取数据。例如。如果当前月份是一月,我需要一月、二月和三月的数据如果当前月份是二月,我需要二月、三月、四月的数据如果当前月份是三月,我需要三月、四月、五月的数据等等。

输出应该是:

MMDDYY  Pagevisted  Username    No. of time[3 M rolling month]  
1/1/2019 Homepage A 4 this include 1 from jan, 3 from march
2/21/2019 AccountPage A 1 Account page opened by A user from current month to next other 2 month i.e. Mar April is only once
2/25/2019 AccountPage B 5 Account page opened by B user from current month to next other 2 month i.e. Mar April is only 5 time
3/1/2019 Homepage A 3 User A in march month opened homepage 3 time, but he didn't opened in following 2 other month i.e. Mar April May
6/2/2019 Submisison C 1
5/5/2019 Homepage D 2
5/2/2019 Articles E 2
7/25/2019 cartpage E 2
8/12/2019 Articles A 7
9/23/2019 Articles A 6

最佳答案

以下适用于 BigQuery 标准 SQL

#standardSQL
SELECT *, SUM(no_of_time) OVER(rolling_3_month_window) AS rolling_3_month
FROM `project.dataset.table`
WINDOW rolling_3_month_window AS (
PARTITION BY username, pagevisited
ORDER BY DATE_DIFF(PARSE_DATE('%m/%d/%Y', mmddyyyy), '1970-01-01', MONTH)
RANGE BETWEEN CURRENT ROW AND 2 FOLLOWING
)

如果适用于您的问题中的示例数据,如下例所示

#standardSQL
WITH `project.dataset.table` AS (
SELECT '1/1/2019' mmddyyyy, 'Homepage' pagevisited, 'A' username, 1 no_of_time, 'January' month UNION ALL
SELECT '2/21/2019', 'AccountPage', 'A', 1, 'February' UNION ALL
SELECT '2/25/2019', 'AccountPage', 'B', 5, 'February' UNION ALL
SELECT '3/1/2019', 'Homepage', 'A', 3, 'March' UNION ALL
SELECT '4/2/2019', 'cartpage', 'B', 2, 'April' UNION ALL
SELECT '5/2/2019', 'AccountPage', 'A', 1, 'May' UNION ALL
SELECT '6/2/2019', 'Submisison', 'C', 1, 'June' UNION ALL
SELECT '5/5/2019', 'Homepage', 'D', 2, 'May' UNION ALL
SELECT '5/2/2019', 'Articles', 'E', 2, 'May' UNION ALL
SELECT '7/25/2019', 'cartpage', 'E', 2, 'July' UNION ALL
SELECT '8/12/2019', 'Articles', 'A', 1, 'August' UNION ALL
SELECT '9/23/2019', 'Articles', 'A', 6, 'September'
)
SELECT *, SUM(no_of_time) OVER(rolling_3_month_window) AS rolling_3_month
FROM `project.dataset.table`
WINDOW rolling_3_month_window AS (
PARTITION BY username, pagevisited
ORDER BY DATE_DIFF(PARSE_DATE('%m/%d/%Y', mmddyyyy), '1970-01-01', MONTH)
RANGE BETWEEN CURRENT ROW AND 2 FOLLOWING
)
-- ORDER BY mmddyyyy

输出为

Row mmddyyyy    pagevisited username    no_of_time  month       rolling_3_month  
1 1/1/2019 Homepage A 1 January 4
2 2/21/2019 AccountPage A 1 February 1
3 2/25/2019 AccountPage B 5 February 5
4 3/1/2019 Homepage A 3 March 3
5 4/2/2019 cartpage B 2 April 2
6 5/2/2019 AccountPage A 1 May 1
7 5/2/2019 Articles E 2 May 2
8 5/5/2019 Homepage D 2 May 2
9 6/2/2019 Submisison C 1 June 1
10 7/25/2019 cartpage E 2 July 2
11 8/12/2019 Articles A 1 August 7
12 9/23/2019 Articles A 6 September 6

关于sql - Bigquery 滚动月份数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62390791/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com