Select
month,
count(*) as custs,
(select
count(distinct cust_id)
from mytable b
where b.month<=a.month) as RunningUniqueCusts
From mytable a
group by month
或者对于月份和时间地区
Select
month,
region,
count(*) as custs,
(select
count(distinct cust_id)
from mytable b
where b.month<=a.month
and b.region=a.region) as RunningUniqueCustsForRegion
From mytable a
group by month, region
2022 年 3 月 3 日更新 以下代码将返回之前未出现过的每个月的唯一客户 ID:
SELECT TM.MONTH_ID, TM.CUST_ID
FROM MYTABLE as TM
WHERE NOT EXISTS
(SELECT 1
FROM MYTABLE as PM
WHERE PM.CUST_ID = TM.CUST_ID
and PM.MONTH < TM.MONTH)
GROUP BY TM.MONTH_ID, TM.CUST_ID
ORDER BY TM.MONTH_ID, TM.CUST_ID
This might do:
Select
month,
count(*) as custs,
(select
count(distinct cust_id)
from mytable b
where b.month<=a.month) as RunningUniqueCusts
From mytable a
group by month
Or for month & region
Select
month,
region,
count(*) as custs,
(select
count(distinct cust_id)
from mytable b
where b.month<=a.month
and b.region=a.region) as RunningUniqueCustsForRegion
From mytable a
group by month, region
Update 3-Mar-2022 The following would return the unique customer ids for each month where they didn't appear previously:
SELECT TM.MONTH_ID, TM.CUST_ID
FROM MYTABLE as TM
WHERE NOT EXISTS
(SELECT 1
FROM MYTABLE as PM
WHERE PM.CUST_ID = TM.CUST_ID
and PM.MONTH < TM.MONTH)
GROUP BY TM.MONTH_ID, TM.CUST_ID
ORDER BY TM.MONTH_ID, TM.CUST_ID
发布评论
评论(1)
这可能会做:
或者对于月份和时间地区
2022 年 3 月 3 日更新
以下代码将返回之前未出现过的每个月的唯一客户 ID:
This might do:
Or for month & region
Update 3-Mar-2022
The following would return the unique customer ids for each month where they didn't appear previously: