使用SSMS 2012但SQL Server 2000。
我有一个查询,其中包括通过冗长的case语句计算的4列。我还需要添加一列,显示这4列的最小值。在最终形式中,只要最后一个col具有最小值,就不需要将4列保留在结果中。下面是我目前使用的代码以及结果示例。在结果样本中,我已经包含了我对Final Tier col的要求。我也是SQL的新手,所以如果有更简洁的方法来完成同样的任务,请随时教我。 :)
Select
,Round(Sum(S.TWin)/Nullif(Count(Distinct S.GamingDate),0),2) as ADT
,Round(Sum(S.Twin)/Nullif(count(Distinct Month(S.GamingDate)),0),2) as AMT
,Round(((Sum(case when S.StatType = 'Slot' then S.CashIn - S.CashOut - S.JackPot Else 0 end)+
Sum(case when S.StatType = 'Pit' then S.CashIn + S.ChipsIn + S.FrontIn + S.CreditIn - S.CashOut Else 0 end))/Count(Distinct S.GamingDate)),2) as ADL
,Round(((Sum(case when S.StatType = 'Slot' then S.CashIn - S.CashOut - S.JackPot Else 0 end)+
Sum(case when S.StatType = 'Pit' then S.CashIn + S.ChipsIn + S.FrontIn + S.CreditIn - S.CashOut Else 0 end))/count(Distinct Month(S.GamingDate))),2) as AML
,case
when Round(Sum(S.TWin)/Nullif(Count(Distinct S.GamingDate),0),2) between 3500 and 1000000 then '1'
when Round(Sum(S.TWin)/Nullif(Count(Distinct S.GamingDate),0),2) between 2000 and 3499.99 then '2'
when Round(Sum(S.TWin)/Nullif(Count(Distinct S.GamingDate),0),2) between 1500 and 1999.99 then '3'
when Round(Sum(S.TWin)/Nullif(Count(Distinct S.GamingDate),0),2) between 1000 and 1499.99 then '4'
when Round(Sum(S.TWin)/Nullif(Count(Distinct S.GamingDate),0),2) between 750 and 999.99 then '5'
when Round(Sum(S.TWin)/Nullif(Count(Distinct S.GamingDate),0),2) between 500 and 749.99 then '6'
when Round(Sum(S.TWin)/Nullif(Count(Distinct S.GamingDate),0),2) between 300 and 499.99 then '7'
when Round(Sum(S.TWin)/Nullif(Count(Distinct S.GamingDate),0),2) between 150 and 299.99 then '8'
when Round(Sum(S.TWin)/Nullif(Count(Distinct S.GamingDate),0),2) between 75 and 149.99 then '9'
when Round(Sum(S.TWin)/Nullif(Count(Distinct S.GamingDate),0),2) between 40 and 74.99 then '10'
when Round(Sum(S.TWin)/Nullif(Count(Distinct S.GamingDate),0),2) between 15 and 39.99 then '11'
Else null
End as "ADT Tier"
,case
when Round(Sum(S.Twin)/Nullif(count(Distinct Month(S.GamingDate)),0),2) between 21000 and 1000000 then '1'
when Round(Sum(S.Twin)/Nullif(count(Distinct Month(S.GamingDate)),0),2) between 12000 and 20999 then '2'
when Round(Sum(S.Twin)/Nullif(count(Distinct Month(S.GamingDate)),0),2) between 9000 and 11999 then '3'
when Round(Sum(S.Twin)/Nullif(count(Distinct Month(S.GamingDate)),0),2) between 6000 and 8999 then '4'
when Round(Sum(S.Twin)/Nullif(count(Distinct Month(S.GamingDate)),0),2) between 4500 and 5999 then '5'
when Round(Sum(S.Twin)/Nullif(count(Distinct Month(S.GamingDate)),0),2) between 3000 and 4499 then '6'
when Round(Sum(S.Twin)/Nullif(count(Distinct Month(S.GamingDate)),0),2) between 1800 and 2999 then '7'
when Round(Sum(S.Twin)/Nullif(count(Distinct Month(S.GamingDate)),0),2) between 900 and 1799 then '8'
when Round(Sum(S.Twin)/Nullif(count(Distinct Month(S.GamingDate)),0),2) between 450 and 899 then '9'
when Round(Sum(S.Twin)/Nullif(count(Distinct Month(S.GamingDate)),0),2) between 240 and 349 then '10'
when Round(Sum(S.Twin)/Nullif(count(Distinct Month(S.GamingDate)),0),2) between 90 and 239 then '11'
Else null
End as "AMT Tier"
,case
when Round(((Sum(case when S.StatType = 'Slot' then S.CashIn - S.CashOut - S.JackPot Else 0 end)+
Sum(case when S.StatType = 'Pit' then S.CashIn + S.ChipsIn + S.FrontIn + S.CreditIn - S.CashOut Else 0 end))/Count(Distinct S.GamingDate)),2)
between 3500 and 1000000 then '1'
when Round(((Sum(case when S.StatType = 'Slot' then S.CashIn - S.CashOut - S.JackPot Else 0 end)+
Sum(case when S.StatType = 'Pit' then S.CashIn + S.ChipsIn + S.FrontIn + S.CreditIn - S.CashOut Else 0 end))/Count(Distinct S.GamingDate)),2)
between 2000 and 3499.99 then '2'
when Round(((Sum(case when S.StatType = 'Slot' then S.CashIn - S.CashOut - S.JackPot Else 0 end)+
Sum(case when S.StatType = 'Pit' then S.CashIn + S.ChipsIn + S.FrontIn + S.CreditIn - S.CashOut Else 0 end))/Count(Distinct S.GamingDate)),2)
between 1500 and 1999.99 then '3'
when Round(((Sum(case when S.StatType = 'Slot' then S.CashIn - S.CashOut - S.JackPot Else 0 end)+
Sum(case when S.StatType = 'Pit' then S.CashIn + S.ChipsIn + S.FrontIn + S.CreditIn - S.CashOut Else 0 end))/Count(Distinct S.GamingDate)),2)
between 1000 and 1499.99 then '4'
when Round(((Sum(case when S.StatType = 'Slot' then S.CashIn - S.CashOut - S.JackPot Else 0 end)+
Sum(case when S.StatType = 'Pit' then S.CashIn + S.ChipsIn + S.FrontIn + S.CreditIn - S.CashOut Else 0 end))/Count(Distinct S.GamingDate)),2)
between 750 and 999.99 then '5'
when Round(((Sum(case when S.StatType = 'Slot' then S.CashIn - S.CashOut - S.JackPot Else 0 end)+
Sum(case when S.StatType = 'Pit' then S.CashIn + S.ChipsIn + S.FrontIn + S.CreditIn - S.CashOut Else 0 end))/Count(Distinct S.GamingDate)),2)
between 500 and 749.99 then '6'
when Round(((Sum(case when S.StatType = 'Slot' then S.CashIn - S.CashOut - S.JackPot Else 0 end)+
Sum(case when S.StatType = 'Pit' then S.CashIn + S.ChipsIn + S.FrontIn + S.CreditIn - S.CashOut Else 0 end))/Count(Distinct S.GamingDate)),2)
between 300 and 499.99 then '7'
when Round(((Sum(case when S.StatType = 'Slot' then S.CashIn - S.CashOut - S.JackPot Else 0 end)+
Sum(case when S.StatType = 'Pit' then S.CashIn + S.ChipsIn + S.FrontIn + S.CreditIn - S.CashOut Else 0 end))/Count(Distinct S.GamingDate)),2)
between 150 and 299.99 then '8'
when Round(((Sum(case when S.StatType = 'Slot' then S.CashIn - S.CashOut - S.JackPot Else 0 end)+
Sum(case when S.StatType = 'Pit' then S.CashIn + S.ChipsIn + S.FrontIn + S.CreditIn - S.CashOut Else 0 end))/Count(Distinct S.GamingDate)),2)
between 75 and 149.99 then '9'
when Round(((Sum(case when S.StatType = 'Slot' then S.CashIn - S.CashOut - S.JackPot Else 0 end)+
Sum(case when S.StatType = 'Pit' then S.CashIn + S.ChipsIn + S.FrontIn + S.CreditIn - S.CashOut Else 0 end))/Count(Distinct S.GamingDate)),2)
between 40 and 74.99 then '10'
when Round(((Sum(case when S.StatType = 'Slot' then S.CashIn - S.CashOut - S.JackPot Else 0 end)+
Sum(case when S.StatType = 'Pit' then S.CashIn + S.ChipsIn + S.FrontIn + S.CreditIn - S.CashOut Else 0 end))/Count(Distinct S.GamingDate)),2)
between 15 and 39.99 then '11'
Else null
End as "ADL Tier"
,case
when Round(((Sum(case when S.StatType = 'Slot' then S.CashIn - S.CashOut - S.JackPot Else 0 end)+
Sum(case when S.StatType = 'Pit' then S.CashIn + S.ChipsIn + S.FrontIn + S.CreditIn - S.CashOut Else 0 end))/count(Distinct Month(S.GamingDate))),2)
between 21000 and 1000000 then '1'
when Round(((Sum(case when S.StatType = 'Slot' then S.CashIn - S.CashOut - S.JackPot Else 0 end)+
Sum(case when S.StatType = 'Pit' then S.CashIn + S.ChipsIn + S.FrontIn + S.CreditIn - S.CashOut Else 0 end))/count(Distinct Month(S.GamingDate))),2)
between 12000 and 20999 then '2'
when Round(((Sum(case when S.StatType = 'Slot' then S.CashIn - S.CashOut - S.JackPot Else 0 end)+
Sum(case when S.StatType = 'Pit' then S.CashIn + S.ChipsIn + S.FrontIn + S.CreditIn - S.CashOut Else 0 end))/count(Distinct Month(S.GamingDate))),2)
between 9000 and 11999 then '3'
when Round(((Sum(case when S.StatType = 'Slot' then S.CashIn - S.CashOut - S.JackPot Else 0 end)+
Sum(case when S.StatType = 'Pit' then S.CashIn + S.ChipsIn + S.FrontIn + S.CreditIn - S.CashOut Else 0 end))/count(Distinct Month(S.GamingDate))),2)
between 6000 and 8999 then '4'
when Round(((Sum(case when S.StatType = 'Slot' then S.CashIn - S.CashOut - S.JackPot Else 0 end)+
Sum(case when S.StatType = 'Pit' then S.CashIn + S.ChipsIn + S.FrontIn + S.CreditIn - S.CashOut Else 0 end))/count(Distinct Month(S.GamingDate))),2)
between 4500 and 5999 then '5'
when Round(((Sum(case when S.StatType = 'Slot' then S.CashIn - S.CashOut - S.JackPot Else 0 end)+
Sum(case when S.StatType = 'Pit' then S.CashIn + S.ChipsIn + S.FrontIn + S.CreditIn - S.CashOut Else 0 end))/count(Distinct Month(S.GamingDate))),2)
between 3000 and 4499 then '6'
when Round(((Sum(case when S.StatType = 'Slot' then S.CashIn - S.CashOut - S.JackPot Else 0 end)+
Sum(case when S.StatType = 'Pit' then S.CashIn + S.ChipsIn + S.FrontIn + S.CreditIn - S.CashOut Else 0 end))/count(Distinct Month(S.GamingDate))),2)
between 1800 and 2999 then '7'
when Round(((Sum(case when S.StatType = 'Slot' then S.CashIn - S.CashOut - S.JackPot Else 0 end)+
Sum(case when S.StatType = 'Pit' then S.CashIn + S.ChipsIn + S.FrontIn + S.CreditIn - S.CashOut Else 0 end))/count(Distinct Month(S.GamingDate))),2)
between 900 and 1799 then '8'
when Round(((Sum(case when S.StatType = 'Slot' then S.CashIn - S.CashOut - S.JackPot Else 0 end)+
Sum(case when S.StatType = 'Pit' then S.CashIn + S.ChipsIn + S.FrontIn + S.CreditIn - S.CashOut Else 0 end))/count(Distinct Month(S.GamingDate))),2)
between 450 and 899 then '9'
when Round(((Sum(case when S.StatType = 'Slot' then S.CashIn - S.CashOut - S.JackPot Else 0 end)+
Sum(case when S.StatType = 'Pit' then S.CashIn + S.ChipsIn + S.FrontIn + S.CreditIn - S.CashOut Else 0 end))/count(Distinct Month(S.GamingDate))),2)
between 240 and 349 then '10'
when Round(((Sum(case when S.StatType = 'Slot' then S.CashIn - S.CashOut - S.JackPot Else 0 end)+
Sum(case when S.StatType = 'Pit' then S.CashIn + S.ChipsIn + S.FrontIn + S.CreditIn - S.CashOut Else 0 end))/count(Distinct Month(S.GamingDate))),2)
between 90 and 239 then '11'
Else null
End as "AML Tier"
From dbo.CDS_STATDAY as S
Where S.GamingDate Between '06/1/2014' and '08/31/2014'
And S.IDType = 'P'
And S.StatType <> 'Poker'
Group by S.Meta_ID
结果
Player ID ADT AMT ADL AML ADT Tier AMT Tier ADL Tier AML Tier Final Tier
114 498.26 4484.31 394.99 3554.90 7 6 7 6 6
144 59.42 257.50 61.46 266.34 10 10 10 10 10
316 0.29 0.29 -13.1 -13.1 NULL NULL NULL NULL NULL
573 3.09 6.18 60 120 NULL NULL 10 11 10
我不确定这是否最好用子查询,嵌套大小写,或者我甚至不知道存在的东西。
答案 0 :(得分:0)
我认为你的方法没有问题,但也许最好在应用程序级而不是查询中处理这类事情。
答案 1 :(得分:0)
首先,将查询转换为视图。 在视图上创建一个新查询,已经计算了字段,您可以将字段相互比较。