sql - generate_series() 在 PostgreSQL 中无法按预期方式使用 sum

标签 sql postgresql join sum generate-series

我有一些名为分类的表,其中包含 classification_indicator_id
我需要对这个 ID 求和并放入 1 天的系列。
我需要添加大约 20 列(使用另一个 classification_indicator_id)。
我修改了一点来自 previous question 的答案:

select
data.d::date as "data",
sum(c.classification_indicator_id)::integer as "Segment1",
sum(c4.classification_indicator_id)::integer as "Segment2",
sum(c5.classification_indicator_id)::integer as "Segment3"
from 
  generate_series(
    '2013-03-25'::timestamp without time zone,
    '2013-04-01'::timestamp without time zone,
    '1 day'::interval
) data(d)
left join classifications c on (data.d::date = c.created::date and c.classification_indicator_id = 3)
left join classifications c4 on (data.d::date = c4.created::date and c4.classification_indicator_id = 4)
left join classifications c5 on (data.d::date = c5.created::date and c5.classification_indicator_id = 5)
group by "data"
ORDER BY "data"

但仍然无法正常工作。每行的 sum 太大,并且在我添加额外的列时增长。在 segment1 中有 4 列的第二个表中,2013-03-26 的数量应该与第一个表等中的数量相同。

 With 3 column                      With 4 columns
data       | Segment1 | Segment2   data       | Segment1 | Segment2 | Segment3
--------------------------------   -------------------------------------------
2013-03-25 | 12       | 16         2013-03-25 | 12       | 16       | 20
--------------------------------   -------------------------------------------
2013-03-26 | 18       | 24         2013-03-26 | 108      | 144      | 180    

最佳答案

作为commented under your previous answer ,您遇到了“代理交叉连接”。
我在这个相关答案中更详细地解释了它:
Two SQL LEFT JOINS produce incorrect result

你的查询应该是这样的:

SELECT d.created AS data
      ,c3.segment1
      ,c4.segment2
      ,c5.segment3
FROM (
   SELECT generate_series('2013-03-25'::date
                         ,'2013-04-01'::date
                         ,interval '1 day')::date AS created
    ) d
LEFT JOIN (
    SELECT created
          ,sum(classification_indicator_id)::integer AS segment1
    FROM   classifications
    WHERE  classification_indicator_id = 3
    GROUP  BY 1
    ) c3 USING (created)
LEFT JOIN (
    SELECT created
          ,sum(classification_indicator_id)::integer AS segment2
    FROM   classifications
    WHERE  classification_indicator_id = 4
    GROUP  BY 1
    ) c4 USING (created)
LEFT JOIN (
    SELECT created
          ,sum(classification_indicator_id)::integer AS segment3
    FROM   classifications
    WHERE  classification_indicator_id = 5
    GROUP  BY 1
    ) c5 USING (created)
ORDER  BY 1;

假设 created 是一个 date,而不是 timestamp

或者,为了更快的查询,因为这已经成为一个话题:

SELECT d.created AS data
      ,count(classification_indicator_id = 3 OR NULL)::int * 3 AS segment1
      ,count(classification_indicator_id = 4 OR NULL)::int * 4 AS segment2
      ,count(classification_indicator_id = 5 OR NULL)::int * 5 AS segment3
FROM (
   SELECT generate_series('2013-03-25'::date
                         ,'2013-04-01'::date
                         ,interval '1 day')::date AS created
    ) d
LEFT   JOIN classifications c USING (created)
GROUP  BY 1
ORDER  BY 1;

关于sql - generate_series() 在 PostgreSQL 中无法按预期方式使用 sum,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15811735/

相关文章:

php - Woocommerce 选择与多个元值匹配的订单

mysql - 使用其他列的值更新列,该列本身在同一个 UPDATE 中全部更新

MySQL 选择连接空值

mysql - 如何从同一 mysql 表上的子查询更新临时字段(避免 MySql 错误 #1093)

sql - SQL查询如何工作?

python - Jupyter Notebook 中的 SQL 列类型

sql - 未经许可使用表的PostgreSQL查询

Qt 10000 Blade

python - 将 Pandas 数据框与列表数据框连接起来

sql - 使用 SQL 将 DD MMM YYYY 转换为日期字段