【问题标题】:Need Help Generating NULL Entries for Months with No Orders需要帮助为没有订单的月份生成 NULL 条目
【发布时间】:2020-10-17 12:07:19
【问题描述】:

我必须创建显示表 (Tbl) 中所有字段的输出,并创建一个附加列,按月计算每个客户的累积总和,(例如,如果客户在 4 月有两次销售,则新列将具有这两行的销售额和任何先前销售额的总和)。我能做的就这么多。

我的问题是每个客户每个月生成行即使他们没有销售,并且累积列仍然正确显示上个月的累积总和。

期望的输出: Picture Link

Customer_ID Order_ID    Order_Date  Order_Amt_Total_USD Month_ID    Cum_Total_By_Month
John        123        4/4/2019      30                  Jun-19          120
John        124        4/12/2019     90                  Jun-19          120
Mark       null         null        null                 Jun-19           0
Sally       150        4/20/2019     50                  Jun-19           50
John        null         null       null                 Jul-19           120
Mark        165        7/7/2019      80                  Jul-19           170
Mark        166        7/7/2019      90                  Jul-19           170
Sally       160        7/5/2019      75                  Jul-19           125
John        null        null         null                Aug-19           120
Mark        null        null         null                Aug-19           170
Sally       null        null         null                Aug-19           125

我将在下面列出代码,但这是一个指向 SQL fiddle 的链接,其中包含示例数据和我正在工作的部分的两个查询(在本网站上各位优秀人士的帮助下)。 http://sqlfiddle.com/#!15/1d86b/11

我可以使用第一个查询按客户和月份生成所需的累积运行总和。

我还可以生成一个基表,在第二个查询中为每个客户的每个月提供一个 month_id。

当月份/客户没有任何销售时,我需要帮助将这两者结合起来,以生成带有空行的所需输出。

有什么想法吗?谢谢!

-- Generates cumulative total by month by Customer, but only shows when they have a sale
SELECT 
    Customer_ID, Order_Date, order_id, Order_Amt_Total_USD,
    to_char(date_trunc('month', Order_Date), 'Mon YYYY') AS mon_text,
    (Select 
     sum(Order_Amt_Total_USD) 
        FROM tbl t2
         WHERE t2.Customer_ID = t.Customer_ID
         AND date_trunc('month', t2.Order_Date) <= t.Order_Date ) AS Cumulative
FROM    tbl t
GROUP BY mon_text, Customer_ID, Order_Date, order_id, Order_Amt_Total_USD
ORDER BY date_trunc('month', Order_Date), Customer_ID, Order_Date
;

-- Generates Proper List of All Month IDs for each Customer from entered date through today
WITH temp AS (
   SELECT date_trunc('month', Order_Date) AS mon_id
  FROM tbl
  )
Select
Customer_ID,
to_char(mon_id, 'Mon YYYY') AS mon_text
From tbl,
generate_series('2015-01-01'::date, now(), interval '1 month') mon_id
LEFT  JOIN temp USING (mon_id)
GROUP BY mon_id,Customer_ID
;

【问题讨论】:

    标签: sql null cumulative-sum


    【解决方案1】:

    根据您的描述,您可以将窗口函数与generate_series()结合起来:

    SELECT c.Customer_ID, mon,
           SUM(Order_Amt_Total_USD) as month_total,
           SUM(SUM(Order_Amt_Total_USD)) OVER (PARTITION BY c.Customer_ID ORDER BY mon) as running_total
    FROM (SELECT DISTINCT Customer_Id FROM tbl) c CROSS JOIN
         generate_series('2015-01-01'::date, now(), interval '1 month') mon LEFT JOIN
         tbl t
         ON t.Customer_Id = c.customer_id and
            date_trunc('month', t.Order_Date) = mon
    GROUP BY c.Customer_ID, mon
    ORDER BY 1, 2;
    

    Here 是一个 SQL Fiddle。

    【讨论】:

    • 非常感谢!这让我大部分时间都在那里,只需要根据我的特定需求调整一些输出格式。
    【解决方案2】:

    下面的示例显示了如何使用 partition by 来实现输出-

    架构

    CREATE TABLE tbl (Order_ID varchar(10), Customer_ID varchar(10), Order_Date date, Order_Amt_Total_USD bigint);
        
    INSERT INTO tbl (Order_ID, Customer_ID, Order_Date, Order_Amt_Total_USD)
    VALUES
        ('100', 'qwe', '2015-08-04', 6),
        ('101', 'qwe', '2015-05-20', 7),
        ('102', 'qwe', '2015-04-08', 8),
        ('103', 'qwe', '2015-04-07', 9),
        ('109', 'aaa', '2015-04-28', 1),
        ('110', 'aaa', '2015-04-28', 2),
        ('111', 'aaa', '2015-05-19', 3),
        ('112', 'aaa', '2015-08-06', 4),
        ('113', 'aaa', '2015-08-27', 5),
        ('114', 'aaa', '2015-08-07', 6)
    

    查询

    select  Order_Date , Customer_ID, Order_Amt_Total_USD,
           sum(Order_Amt_Total_USD) over (partition by Customer_ID order by Order_Date) as cumulative
    from tbl;
    

    【讨论】:

      猜你喜欢
      • 2015-03-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-01-25
      • 1970-01-01
      • 1970-01-01
      • 2020-12-25
      • 2016-03-14
      相关资源
      最近更新 更多