Sql 如何按报告周创建事务表?
我对SQL非常陌生,我正在尝试通过报告周开始日期(星期一)来创建一个新表,并根据每个事务的报告周开始日期来匹配它们。如何在Google BigQuery SQL中做到这一点 我的桌子Sql 如何按报告周创建事务表?,sql,google-bigquery,Sql,Google Bigquery,我对SQL非常陌生,我正在尝试通过报告周开始日期(星期一)来创建一个新表,并根据每个事务的报告周开始日期来匹配它们。如何在Google BigQuery SQL中做到这一点 我的桌子 Account_ID | Order_ID | Reporting_week_start_date 001 | 1001 | 31 Dec 2018 001 | 1002 | 28 Jan 2019
Account_ID | Order_ID | Reporting_week_start_date
001 | 1001 | 31 Dec 2018
001 | 1002 | 28 Jan 2019
001 | 1003 | 25 Feb 2019
002 | 1004 | 31 Dec 2018
002 | 1005 | 28 Jan 2019
002 | 1006 | 25 Feb 2019
期望输出
Reporting_week_start_date | Account_ID | Order_ID
31 Dec 2018 | 001 | 1001 #Transaction 1 (Account_ID 001)
07 Jan 2019 | |
14 Jan 2019 | |
21 Jan 2019 | |
28 Jan 2019 | 001 | 1002 #Transaction 2 (Account_ID 001)
04 Feb 2019 | |
11 Feb 2019 | |
18 Feb 2019 | |
25 Feb 2019 | 001 | 1003 #Transaction 3 (Account_ID 001)
04 Mar 2019 | |
11 Mar 2019 | |
18 Mar 2019 | |
31 Dec 2018 | 002 | 1004 #Transaction 1 (Account_ID 002)
07 Jan 2019 | |
14 Jan 2019 | |
21 Jan 2019 | |
28 Jan 2019 | 002 | 1005 #Transaction 2 (Account_ID 002)
04 Feb 2019 | |
11 Feb 2019 | |
18 Feb 2019 | |
25 Feb 2019 | 002 | 1006 #Transaction 3 (Account_ID 002)
04 Mar 2019 | |
11 Mar 2019 | |
18 Mar 2019 | |
... to current_date()
这是你想要的吗
select wk, t.account_id, t.order_id
from unnest(generate_date_array(date('2018-12-31'), date('2019-03-18'), interval 1 week)) wk cross join
(select distinct account_id
from t
) a left join
t
on t.account_id = a.account_id and
t.reporting_week_start_date = wk
order by a.account_id, wk;
如果您想对每个
账户ID
和订单ID
的日期金额进行分组,我认为此查询可以帮助您获得所需的内容
为了测试查询,首先我创建了一个包含数据的新表,如您的表:
CREATE TABLE `project.dataset.reporting_week` (
`Account_ID` STRING,
`Order_ID` STRING,
`Reporting_week_start_date` DATE);
INSERT INTO `project.dataset.reporting_week` VALUES
('001', '1001', PARSE_DATE('%Y/%m/%d', '2018/12/31')),
('001', '1001', PARSE_DATE('%Y/%m/%d', '2019/01/07')),
('001', '1001', PARSE_DATE('%Y/%m/%d', '2019/01/14')),
('001', '1001', PARSE_DATE('%Y/%m/%d', '2019/01/21')),
('001', '1002', PARSE_DATE('%Y/%m/%d', '2019/01/28')),
('001', '1002', PARSE_DATE('%Y/%m/%d', '2019/02/04')),
('001', '1002', PARSE_DATE('%Y/%m/%d', '2019/02/11')),
('001', '1002', PARSE_DATE('%Y/%m/%d', '2019/02/18')),
('001', '1003', PARSE_DATE('%Y/%m/%d', '2019/02/25')),
('001', '1003', PARSE_DATE('%Y/%m/%d', '2019/03/04')),
('001', '1003', PARSE_DATE('%Y/%m/%d', '2019/03/11')),
('001', '1003', PARSE_DATE('%Y/%m/%d', '2019/03/18')),
('002', '1004', PARSE_DATE('%Y/%m/%d', '2018/12/31')),
('002', '1004', PARSE_DATE('%Y/%m/%d', '2019/01/07')),
('002', '1004', PARSE_DATE('%Y/%m/%d', '2019/01/14')),
('002', '1004', PARSE_DATE('%Y/%m/%d', '2019/01/21')),
('002', '1005', PARSE_DATE('%Y/%m/%d', '2019/01/28')),
('002', '1005', PARSE_DATE('%Y/%m/%d', '2019/02/04')),
('002', '1005', PARSE_DATE('%Y/%m/%d', '2019/02/11')),
('002', '1005', PARSE_DATE('%Y/%m/%d', '2019/02/18')),
('002', '1006', PARSE_DATE('%Y/%m/%d', '2019/02/25')),
('002', '1006', PARSE_DATE('%Y/%m/%d', '2019/03/04')),
('002', '1006', PARSE_DATE('%Y/%m/%d', '2019/03/11')),
('002', '1006', PARSE_DATE('%Y/%m/%d', '2019/03/18'))
因此,我们可以运行查询并从上表创建新表
CREATE TABLE `project.dataset.reporting_week2` AS
SELECT new_account, new_order, ARRAY(SELECT Reporting_week_start_date FROM
`project.dataset.reporting_week` WHERE Account_ID = new_account and Order_ID =
new_order ) as DATES
FROM (SELECT distinct account_id as new_account, new_order FROM
`project.dataset.reporting_week`, (SELECT distinct order_id as new_order FROM
`project.dataset.reporting_week`)
where order_id = new_order)
输出类似于:
@Junior coder,您将使用以下查询获得预期结果。我以21天为例。代替21天,您可以将日差设置为(CurrentDate-reporting\u start\u date) 输出。。
如果一周内有多个订单怎么办?
WITH
DS AS (select Account_ID, Order_ID ,Reporting_week_start_date as start_date, DATE_ADD(Reporting_week_start_date, INTERVAL 21 DAY) as advanced_date, from pay_recon_cl_dataset.reporting_week)
select GENERATE_DATE_ARRAY(start_date,advanced_date, interval 1 week),Account_ID,Order_ID from DS;