使用PostgresQL运行计数总计

问题描述 投票:1回答:1

我非常接近这个解决方案,但我需要一点帮助才能完成。

我试图获得client_ids的运行计数,无论日期如何,但是我需要日期和ID仍然出现在我的结果中以验证所有内容。

我找到了解决方案here的一部分,但未能根据我的需要对其进行足够的修改。

这是答案应该是什么,依次计算client_ids的出现次数:

id  client_id   deliver_on  running_total
1   138         2017-10-01  1
2   29          2017-10-01  1
3   138         2017-10-01  2
4   29          2013-10-02  2
5   29          2013-10-02  3
6   29          2013-10-03  4
7   138         2013-10-03  3

但是,这是我得到的:

id  client_id   deliver_on  running_total
1   138         2017-10-01  1
2   29          2017-10-01  1
3   138         2017-10-01  1
4   29          2013-10-02  3
5   29          2013-10-02  3
6   29          2013-10-03  1
7   138         2013-10-03  2

代码不计算client_id按顺序出现的次数,而是计算id出现在上一个日期范围内的时间。

这是我的代码,任何帮助将不胜感激。

谢谢,

SELECT n.id, n.client_id, n.deliver_on, COUNT(n.client_id) AS "running_total"

FROM orders n 

LEFT JOIN orders o 
    ON (o.client_id = n.client_id 
            AND n.deliver_on > o.deliver_on)          

GROUP BY n.id, n.deliver_on, n.client_id
ORDER BY n.deliver_on ASC

*编辑与答案*

我最终解决了自己的问题。这是带注释的解决方案:

-- Set "1" for counting to be used later

WITH DATA AS (

SELECT

   orders.id, 
   orders.client_id, 
   orders.deliver_on,
   COUNT(1) -- Creates a column of "1" for counting the occurrences

   FROM orders

   GROUP BY 1

   ORDER BY deliver_on, client_id

)

SELECT

   id,
   client_id,
   deliver_on,
   SUM(COUNT) OVER (PARTITION BY client_id 
                           ORDER BY client_id, deliver_on 
                           ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) -- Counts the sequential client_ids based on the number of times they appear

 FROM DATA
postgresql
1个回答
1
投票

只是答案贴出来关闭这个问题:

-- Set "1" for counting to be used later
WITH DATA AS (

SELECT

   orders.id, 
   orders.client_id, 
   orders.deliver_on,
   COUNT(1) -- Creates a column of "1" for counting the occurrences

   FROM orders

   GROUP BY 1

   ORDER BY deliver_on, client_id

)

SELECT

   id,
   client_id,
   deliver_on,
   SUM(COUNT) OVER (PARTITION BY client_id 
                           ORDER BY client_id, deliver_on 
                           ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) -- Counts the sequential client_ids based on the number of times they appear

 FROM DATA
© www.soinside.com 2019 - 2024. All rights reserved.