【发布时间】:2014-11-26 19:28:01
【问题描述】:
使用 SSMS 2012 但使用 SQL Server 2000。
我有一个查询,其中包含通过冗长的案例语句计算的 4 列。我需要再添加一列来显示这 4 列中的最小值。在最终形式中,只要最后一列具有最小值,就不需要 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
我不确定这是否最好使用子查询、嵌套大小写或我什至不知道存在的东西来完成。
【问题讨论】:
-
您使用的是哪个 RDMS?
-
我是 sql 的超级新手,所以我不确定你所说的 RDMS 是什么意思,但我想你想知道我正在使用 T-SQL。
标签: sql case sql-server-2000 min