计算不同的每月运行总计

发布于 2025-01-10 08:01:10 字数 1432 浏览 0 评论 0原文

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

硬不硬你别怂 2025-01-17 08:01:10

这可能会做:

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文