缺少group by值时不显示SQL累积和

缺少group by值时不显示SQL累积和,sql,window,amazon-redshift,partition,Sql,Window,Amazon Redshift,Partition,我有一些月度数据如下: Month | Category | Monthly Value 2020-07-01| Food | 1 2020-07-01| Entertainment | 4 2020-08-01| Entertainment | 2 2020-09-01| Entertainment | 1 Month | Category | Cumulative Sum 2020-07-01

我有一些月度数据如下:

Month     | Category          | Monthly Value
2020-07-01| Food              | 1
2020-07-01| Entertainment     | 4
2020-08-01| Entertainment     | 2
2020-09-01| Entertainment     | 1
Month       |  Category     | Cumulative Sum
2020-07-01  |  Food         |   1
2020-08-01  |  Food         |   1
2020-09-01  |  Food         |   1
2020-07-01  | Entertainment |   4
2020-08-01  | Entertainment |   6
2020-09-01  | Entertainment |   7
    SELECT
      month
    , category
    , sum("monthly value") OVER (PARTITION BY "category" ORDER BY "month" ASC ROWS UNBOUNDED PRECEDING) AS "Cumulative Sum"
    from (
select date_trunc('month', daily_date) as month, category, sum(daily_value) as "monthly value"
from sample_table 
group by date_trunc('month', daily_date) as month, category)
我想计算每个类别的累计总和,结果如下:

Month     | Category          | Monthly Value
2020-07-01| Food              | 1
2020-07-01| Entertainment     | 4
2020-08-01| Entertainment     | 2
2020-09-01| Entertainment     | 1
Month       |  Category     | Cumulative Sum
2020-07-01  |  Food         |   1
2020-08-01  |  Food         |   1
2020-09-01  |  Food         |   1
2020-07-01  | Entertainment |   4
2020-08-01  | Entertainment |   6
2020-09-01  | Entertainment |   7
    SELECT
      month
    , category
    , sum("monthly value") OVER (PARTITION BY "category" ORDER BY "month" ASC ROWS UNBOUNDED PRECEDING) AS "Cumulative Sum"
    from (
select date_trunc('month', daily_date) as month, category, sum(daily_value) as "monthly value"
from sample_table 
group by date_trunc('month', daily_date) as month, category)
我正在编写窗口总和查询,如下所示:

Month     | Category          | Monthly Value
2020-07-01| Food              | 1
2020-07-01| Entertainment     | 4
2020-08-01| Entertainment     | 2
2020-09-01| Entertainment     | 1
Month       |  Category     | Cumulative Sum
2020-07-01  |  Food         |   1
2020-08-01  |  Food         |   1
2020-09-01  |  Food         |   1
2020-07-01  | Entertainment |   4
2020-08-01  | Entertainment |   6
2020-09-01  | Entertainment |   7
    SELECT
      month
    , category
    , sum("monthly value") OVER (PARTITION BY "category" ORDER BY "month" ASC ROWS UNBOUNDED PRECEDING) AS "Cumulative Sum"
    from (
select date_trunc('month', daily_date) as month, category, sum(daily_value) as "monthly value"
from sample_table 
group by date_trunc('month', daily_date) as month, category)
但是,我得到如下信息:

Month           |  Category     | Cumulative Sum
    2020-07-01  |  Food         |   1
    2020-07-01  | Entertainment |   4
    2020-08-01  | Entertainment |   6
    2020-09-01  | Entertainment |   7
为什么在
2020-08-01
2020-09-01
的几个月内,“食品”类别的累计金额没有显示出来?如何使结果按预期显示(如第二张表所示)

顺便说一句,我用的是红移。
谢谢

使用
交叉连接
生成行,然后
左连接
引入值:

select m.month, c.category, t.monthly_value,
       sum(t.monthly_value) over (partition by c.category order by m.month) as running_monthly_value
from (select distinct month from t) m cross join
     (select distinct category from t) c left join
     t
     on t.month = m.month and t.category = c.category;