我有一个包含每周客户和商店信息的数据集,
问题 - 我必须计算一些特征,例如截至当前周的过去 1、2、3、4、5、6..so 的总独立客户总数。
我在窗口函数上使用 count distinct of customers 列时遇到错误 -
我尝试使用 concat 函数来创建数组,但它也不起作用 - 感谢您的帮助!
SELECT STORE,WEEK,
count(distinct Customers) over (partition by STORE order by WEEK rows between 1 preceding and 1 preceding) as last_1_week_customers,
count(distinct Customers) over (partition by STORE order by WEEK rows between 2 preceding and 2 preceding) as last_2_week_customers
from TEST_TABLE
group by STORE,WEEK
错误- SQL 编译错误:distinct 不能与窗口框架或订单一起使用。
我该如何解决这个错误?
输入
CREATE TABLE TEST_TABLE (STORE STRING,WEEK STRING,Customers STRING);
INSERT INTO TEST_TABLE VALUES
('A','1','AA'),
('A','1','DD'),
('A','2','AA'),
('A','2','BB'),
('A','2','CC'),
('A','3','AA');
输出
答案 0 :(得分:0)
嗯...我认为你在这里根本不需要窗口函数...
首先,我们可以从一个简单的分组开始:
select
store,
week,
count(distinct customers) as cnt
from
test_table
where
week >= [this week's number minus 5]
group by
store, week
这将产生一个简单的表格:
商店 | 周 | cnt |
---|---|---|
A | 1 | 2 |
A | 2 | 3 |
A | 3 | 1 |
此时我想请您考虑一下这是否已经足够了。可能您已经可以将这种格式的数据用于您需要的任何目的。但如果不是,那么我们可以进一步修改它以获得“旋转”输出。
在此查询中,将 ${w}
替换为本周的数字:
select
store,
count(distinct case when week=${w} then customers else null end) as cnt_now,
count(distinct case when week=${w-1} then customers else null end) as cnt_minus_1,
count(distinct case when week=${w-2} then customers else null end) as cnt_minus_2,
count(distinct case when week=${w-3} then customers else null end) as cnt_minus_3,
count(distinct case when week=${w-4} then customers else null end) as cnt_minus_4,
count(distinct case when week=${w-5} then customers else null end) as cnt_minus_5
from
test_table
where
week >= {$w-5}
group by
store
记住 - COUNT()
和 COUNT(DISTINCT)
只计算 NON-NULL 值。
商店 | cnt_now | cnt_minus_1 | cnt_minus_2 | cnt_minus_3 | cnt_minus_4 | cnt_minus_5 |
---|---|---|---|---|---|---|
A | 1 | 2 | 3 | 4 | 5 | 6 |
B | 9 | 8 | 7 | 6 | 5 | 4 |