在 PostgreSQL 中按月透视用户收入

Pivoting user revenues by month in PostgreSQL

在 PostgreSQL 中编写查询时,我有以下语法。这个查询工作得很好。我唯一的问题是下面出现的空值。

我想在此查询中使用 Coalesce(),但我被卡住了。有没有办法调整此查询以便我可以将 Coalesce 添加到此查询?

-- Import tablefunc
CREATE EXTENSION IF NOT EXISTS tablefunc;

SELECT * FROM CROSSTAB($$
  SELECT
    user_id,
    DATE_TRUNC('month', order_date) :: DATE AS delivr_month,
    SUM(meal_price * order_quantity) :: FLOAT AS revenue
  FROM meals
  JOIN orders ON meals.meal_id = orders.meal_id
 WHERE user_id IN (0, 1, 2, 3, 4)
   AND order_date < '2018-09-01'
 GROUP BY user_id, delivr_month
 ORDER BY user_id, delivr_month;
$$)
-- Select user ID and the months from June to August 2018
AS ct (user_id INT,
       "2018-06-01" FLOAT,
       "2018-07-01" FLOAT,
       "2018-08-01" FLOAT)     
ORDER BY user_id ASC;

输出:

SELECT 
       user_id,
       coalesce("2018-06-01", 0.0) "2018-06-01",
       coalesce("2018-07-01", 0.0) "2018-07-01",
       coalesce("2018-08-01", 0.0) "2018-08-01"
 FROM CROSSTAB($$
  SELECT
    user_id,
    DATE_TRUNC('month', order_date) :: DATE AS delivr_month,
    SUM(meal_price * order_quantity) :: FLOAT AS revenue
  FROM meals
  JOIN orders ON meals.meal_id = orders.meal_id
 WHERE user_id IN (0, 1, 2, 3, 4)
   AND order_date < '2018-09-01'
 GROUP BY user_id, delivr_month
 ORDER BY user_id, delivr_month;
$$)
-- Select user ID and the months from June to August 2018
AS ct (user_id INT,
       "2018-06-01" FLOAT,
       "2018-07-01" FLOAT,
       "2018-08-01" FLOAT)     
ORDER BY user_id ASC;