【问题标题】:Get A Running Sum For Rep By Day按天获取代表的运行总和
【发布时间】:2021-01-26 23:08:02
【问题描述】:

我是 psql 的新手,想知道是否可以在查询方面获得帮助。

我有一个显示如下的当前表格:

Index ID           Date Sent          Tag      Name     Tad ID      Cat
6   20306   "2020-09-18 12:02:58"   "9902"  "Michelle"  "9902"  "Servicing"
7   20510   "2020-08-27 11:23:01"   "9896"  "Jessica"   "9896"  "Servicing"
8   20582   "2020-09-21 15:24:25"   "9902"  "Michelle"  "9902"  "Servicing"
9   21196   "2020-07-22 11:42:10"   "9902"  "Michelle"  "9902"  "Servicing"
10  26548   "2020-08-03 15:16:26"   "9902"  "Michelle"  "9902"  "Servicing"
11  26924   "2020-08-28 15:49:29"   "9902"  "Michelle"  "9902"  "Servicing"
12  30310   "2020-09-24 09:44:06"   "9900"  "Godfrey"   "9900"  "Servicing"

我想知道每个人(姓名)在过去 30 天内总共有多少条记录?

我知道我当前的查询可能还没有结束,但我仍会向您展示我目前正在处理的内容:

select date_sent, Count(Distinct id) as total_leads, name from finwell_leads
group by date_sent, name

这是我希望看到的:

"2020-10-01 12:02:58" Michelle 0
"2020-10-01 12:02:58" Jessica  1
"2020-10-01 12:02:58" Godfrey  0
"2020-10-02 12:02:58" Michelle 2
"2020-10-02 12:02:58" Jessica  1
"2020-10-02 12:02:58" Godfrey  1
"2020-10-05 12:02:58" Michelle 5
"2020-10-05 12:02:58" Jessica  3
"2020-10-05 12:02:58" Godfrey  1
"2020-10-06 12:02:58" Michelle 5
"2020-10-06 12:02:58" Jessica  4
"2020-10-06 12:02:58" Godfrey  1
"2020-10-07 12:02:58" Michelle 6
"2020-10-07 12:02:58" Jessica  5
"2020-10-07 12:02:58" Godfrey  2

如何按人检索每天的累计记录?

谢谢!

【问题讨论】:

    标签: sql postgresql datetime count left-join


    【解决方案1】:

    您想要的结果似乎与您的样本数据不一致。但是,如果我没记错的话,您可以将不同的名称与该期间的所有日期交叉连接,然后将表格与left join 一起使用,最后聚合:

    select d.dt, n.name, count(t.name) cnt
    from (select distinct name from mytable) n
    cross join generate_series(date '2020-01-01', date '2020-01-07', '1 day') d(dt)
    left join mytable t 
        on  t.name = n.name
        and t.date_sent >= d.dt - interval '30 day'
        and t.date_sent <= d.dt
    group by d.dt, n.name
    

    【讨论】:

      猜你喜欢
      • 2015-05-06
      • 2015-02-13
      • 2020-06-29
      • 1970-01-01
      • 2016-07-23
      • 1970-01-01
      • 2023-04-02
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多