需要帮助生成几个月没有订单的空条目吗

slmsl1lt  于 2021-07-24  发布在  Java
关注(0)|答案(2)|浏览(410)

我必须创建显示表(tbl)中所有字段的输出,并创建一个额外的列来按月计算每个客户的累计总和(例如,如果一个客户在4月份有两次销售,那么新列将在两行上显示这些销售和任何以前的销售的总和)。我能做的就那么多。
我的问题是每个月为每个客户生成行,即使他们没有销售,但仍然让累计列正确显示上个月的累计金额。
所需输出:图片链接

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摆弄示例数据和两个查询我所处理的部分(在这个站点上的优秀人员的帮助下)。http://sqlfiddle.com/#!2011年1月15日
我可以使用第一个查询按客户和月份生成所需的累计运行总和。
我还可以生成一个基表,在第二个查询中为每个月的每个客户提供一个month\u 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
;
hs1ihplo

hs1ihplo1#

根据您的描述,可以将窗口函数与 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;

这是一个sql小提琴。

qxgroojn

qxgroojn2#

下面的示例显示了如何使用partitionby来实现输出-
架构

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;

相关问题