【问题标题】:Select and assign 0 for the rows which does not have any data为没有任何数据的行选择并分配 0
【发布时间】:2021-10-29 02:30:12
【问题描述】:

我们有客户要求,如果对于特定条件,没有可用数据,则在从 DB 中选择列时分配 0。

下面是表结构。我们需要从今天开始提取过去 6 个月的数据,其中性别为“M”。如果某个月没有“男性”/“女性”(根据 where 条件)参与者,则显示该月的 0 个参与者。

  yearMonth participants gender
    202101     1            F
    202102     0            M  
    202103     0            F
    202003     0            M
    202104     0            F
    202105     0            F
    202105     30           F
    202105     5            M
    202106     22           F
    202106     20           M
    202107     14           F
    202108     29           M 

没有任何where条件,查询如下-

select yearMonth, sum(participants) as participants 
from table_name 
where yearMonth
between 202104 and 202108  group by yearMonth;

此查询返回 -

  yearMonth participants 
    202103     0
    202104     0            
    202105     35           
    202106     42           
    202107     14           
    202108     29     

当我们添加一个 where 条件比如 -

select yearMonth, sum(participants) as participants 
from table_name 
where yearMonth
between 202104 and 202108 
where gender = 'M' group by yearMonth;

返回

  yearMonth participants           
    202105     5           
    202106     20           
    202107     14           
    202108     29  

但要求是,它还应该返回 202103 和 202104,其中 0 作为参与者。

  yearMonth participants 
    202103     0
    202104     0            
    202105     5           
    202106     20           
    202107     14           
    202108     29   

我们可以使用 Postgre SQL 来实现吗?如果可以,请帮助我。

【问题讨论】:

  • 要求不为某些行选择0,即使是没有记录的组合也返回分组结果。即使不存在数据,这也需要创建数据。
  • @PanagiotisKanavos 是的,你是对的。谢谢你的指正。
  • 最简单的方法是在Date 列上使用日历表和RIGHT JOIN。这将为缺少的日期生成“空”行。日历表包含未来的所有日期,例如 10 到 20 年,以及年、月、季度、学期等的额外列。这也使分组变得更加容易和快捷。
  • 能否举个例子。
  • 与您的问题无关,但是:Postgres 9.1 和 9.5 是 no longer supported 您应该尽快计划升级。

标签: sql postgresql postgresql-9.1 postgresql-9.5


【解决方案1】:

使用日历表方法:

WITH dates AS (
    SELECT CAST('2021-01-01' AS DATE) + (n || ' month')::INTERVAL dt
    FROM generate_series(0, 11) n
)

SELECT
    TO_CHAR(d.dt, 'YYYYMM')::int AS yearMonth,
    SUM(t.participants) FILTER (WHERE t.gender = 'M') AS male_participants
FROM dates d
LEFT JOIN table_name t
    ON t.yearMonth = d.yearMonth
WHERE
    TO_CHAR(d.dt, 'YYYYMM')::int BETWEEN 202101 AND 202108 AND        
GROUP BY
    TO_CHAR(d.dt, 'YYYYMM')::int;

【讨论】:

    【解决方案2】:

    您可以使用条件聚合

    select yearMonth, sum(case when gender = 'M' then participants else 0 end) as participants 
    from table_name 
    where yearMonth
    between 202104 and 202108 
    group by yearMonth;
    

    只有在今年的原始表中根本没有任何参与者 raw 时,才能缺少 yearMonth raw。

    【讨论】:

      【解决方案3】:

      一种方法是条件聚合,在 Postgres 中是这样的:

      select yearMonth,
             sum(participants) filter (where gender = 'M') as male_participants 
      from table_name 
      where yearMonth between 202104 and 202108 
      group by yearMonth;
      

      但是,你的情况是:

      我们需要从今天提取过去 6 个月的数据,其中性别为“M”。

      为此,我建议generate_series() 以便自动比较日期:

      select v.yearMonth, 
             coalesce(sum(participants), 0) as male_participants
      from generate_series(0, 5, 1) gs(n) cross join lateral
           (values (to_char(date_trunc(month, current_date) - gs.n * interval '1 month', 'YYYYMM')::int)
           ) v(yearMonth) left join
           table_name t
           on t.yearMonth = v.yearMonth::int and
              t.gender = 'M'
      group by v.yearMonth
      order by v.yearmonth;
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2016-07-03
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-03-30
        相关资源
        最近更新 更多