【发布时间】:2018-02-02 15:54:09
【问题描述】:
如图所示:
这里我需要根据一个条件进行累加:
我有
Column 1: Sales Date
Column 2: Warranty End Date
Column 3: Qty of Sales
Column 4: Cumulative Sales Qty
现在是第 5 列:在保修期内,此处给出了错误的值 当保修日期完成 365 天(到期)时,我需要在保修期内计数列中减去该到期日发生的销售额,类似于保修到期后的接下来几天的计数开始减少。
例如图中: 2014 年 1 月 1 日的累计销售额为 246,当它达到 2015 年 1 月 1 日时,我需要从 '96502' 中减去 '246',这是直到 的累计销售额>2015 年 1 月 1 日。所以应该是 96256
我们如何在SQL中实现'Under保修计数'列的逻辑
我的查询:
SELECT a."sales_year",
a."sales_billing_date",
a."warranty_end_date",
a."sum_val",
a."order_qty",
CASE
WHEN Sum(Days_between (a."warranty_end_date", a."sales_billing_date"))
< 365
THEN a."sum_val"
ELSE ( ( a."sum_val" ) - ( b."sum_val" ) )
END AS "Under_Warranty"
FROM "_SYS_BIC"."pal/cv_pal_tbf_sales" a,
"_SYS_BIC"."pal/cv_pal_tbf_sales" b
GROUP BY a."sales_year",
a."sales_billing_date",
a."warranty_end_date",
a."order_qty",
b."sum_val",
a."sum_val";
【问题讨论】: