这是我正在使用的查询。
DailySales_YTD*DaysRemYr
它现在搞砸了,但我想要实现的是一个计算列,说Projected_Revenue,由
计算 WHEN Product = 'WATCH'
THEN (DaysRemYr-125)
ELSE DaysRemYr
但是,我想创建一个新的“DaysRemYr”列,称之为DaysRem,逻辑是
((dailysales_YTD*DaysRem)+(SumYTDRev)) as ProjRev
这样我就可以使用这个新列来计算下面给出的另一列
Select
Product, Number,
a calculated column which is based on the CASE condition as shown before lets call it DaysRem,
(SUM(DailySales_YTD*DaysRem)+SUM(SUMYTDRev)) as ProjRev
from report
为了清除任何混淆和总结,我试图创建一个Select语句,其中一列是通过将现有列与有条件的新计算列相乘来计算的。
所以,这个想法就是说
Product Number SUMYTDRev dailySales_YTD DaysRemYR
WHEEL 2533 9370.58 112.90 170
WHEEL 8213 3206.08 38.63 170
WHEEL 6475 22770 274.34 170
WHEEL 6475 80608.01 971.18 170
WATCH 8213 146007.2 1759.12 170
WATCH 1177 11500.9 138.57 170
WATCH 1177 39006 469.95 170
WATCH 2371 446990 5385.42 170
WATCH 2371 69005.4 831.39 170
WATCH 204 260008 3132.63 170
BIKE 441 60000 722.89 170
BIKE 6815 18300 220.48 170
BIKE 8918 39900 480.72 170
我想知道它是否可能。
以下是一些有用的示例数据。
group_by
答案 0 :(得分:1)
您可以在计算中使用case
表达式。
Select Product, Number,
CASE WHEN Product = 'WATCH' THEN DaysRemYr-125 ELSE DaysRemYr END as DaysRem,
SUM(DailySales_YTD*CASE WHEN Product = 'WATCH' THEN DaysRemYr-125 ELSE DaysRemYr END)+SUM(SUMYTDRev) as ProjRev
From report
Group by Product,Number,CASE WHEN Product = 'WATCH' THEN DaysRemYr-125 ELSE DaysRemYr END
如果您不想多次指定计算,请使用派生表进行计算。
Select Product, Number, DaysRem,
SUM(DailySales_YTD*DaysRem)+SUM(SUMYTDRev) as ProjRev
From (select r.*,CASE WHEN Product = 'WATCH' THEN DaysRemYr-125 ELSE DaysRemYr END as DaysRem
from report r) r
Group by Product,Number,DaysRem