Sql 多个案例陈述的最小值
使用SSMS 2012,但使用SQL Server 2000 我有一个查询,其中有4列是通过冗长的case语句计算出来的。我需要再添加一列,显示这4列中的最小值。在最终形式中,只要最后一列的值最小,则不需要在结果中保留4列。下面是我目前使用的代码和一个结果示例。在结果样本中,我已经包括了我想要的最后一层列。我也是SQL新手,因此如果有更干净的方法来完成同样的任务,请随时教我。:)Sql 多个案例陈述的最小值,sql,case,sql-server-2000,min,Sql,Case,Sql Server 2000,Min,使用SSMS 2012,但使用SQL Server 2000 我有一个查询,其中有4列是通过冗长的case语句计算出来的。我需要再添加一列,显示这4列中的最小值。在最终形式中,只要最后一列的值最小,则不需要在结果中保留4列。下面是我目前使用的代码和一个结果示例。在结果样本中,我已经包括了我想要的最后一层列。我也是SQL新手,因此如果有更干净的方法来完成同样的任务,请随时教我。:) 我不确定这是否最好用子查询、嵌套大小写或我甚至不知道存在的东西来完成。我不认为您的方法有什么问题,但也许最好在应用程
我不确定这是否最好用子查询、嵌套大小写或我甚至不知道存在的东西来完成。我不认为您的方法有什么问题,但也许最好在应用程序级别而不是在查询中处理此类问题。首先,将查询转换为视图。
首先,在视图上创建一个新的查询,其中字段已经计算完毕,您可以相互比较这些字段。您使用的是哪种RDM?我对sql非常陌生,所以我不太清楚您所说的RDM是什么意思,但我想您想知道我正在使用T-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