X和Y之间的货币价值是多少?

时间:2014-10-01 17:12:29

标签: sql case currency

这是一个简单的过程,使用大概匹配的vlookup excel但我无法使它与SQl一起工作由于某种原因我确信我正在这么努力,任何帮助将不胜感激。以下是我得到的结果示例:

Player ID    ADT         ADT Tier
103         31.25        2
112          6.03        6
114        498.26        7
117       1330.82        4
131         10.01        NULL

以下是该示例的样子:

Player ID    ADT         ADT Tier
103         31.25        11
112          6.03        NULL
114        498.26        7
117       1330.82        4
131         10.01        NULL

以下是我尝试使用的代码。

Select S.Meta_ID as "Player ID"
,Round(Sum(S.TWin)/Nullif(Count(Distinct S.GamingDate),0),2) as ADT
,case 
    when cast(Round(Sum(S.TWin)/Nullif(Count(Distinct S.GamingDate),0),2)as varchar) between '3500' and '1000000' then '1'
    when cast(Round(Sum(S.TWin)/Nullif(Count(Distinct S.GamingDate),0),2)as varchar) between '2000' and '3499.99' then '2'
    when cast(Round(Sum(S.TWin)/Nullif(Count(Distinct S.GamingDate),0),2)as varchar) between '1500' and '1999.99' then '3'
    when cast(Round(Sum(S.TWin)/Nullif(Count(Distinct S.GamingDate),0),2)as varchar) between '1000' and '1499.99' then '4'
    when cast(Round(Sum(S.TWin)/Nullif(Count(Distinct S.GamingDate),0),2)as varchar) between '750'  and '999.99'  then '5'
    when cast(Round(Sum(S.TWin)/Nullif(Count(Distinct S.GamingDate),0),2)as varchar) between '500'  and '749.99'  then '6'
    when cast(Round(Sum(S.TWin)/Nullif(Count(Distinct S.GamingDate),0),2)as varchar) between '300'  and '499.99'  then '7'
    when cast(Round(Sum(S.TWin)/Nullif(Count(Distinct S.GamingDate),0),2)as varchar) between '150'  and '299.99'  then '8'
    when cast(Round(Sum(S.TWin)/Nullif(Count(Distinct S.GamingDate),0),2)as varchar) between '75'   and '149.99'  then '9'
    when cast(Round(Sum(S.TWin)/Nullif(Count(Distinct S.GamingDate),0),2)as varchar) between '40'   and '74.99'   then '10'
    when cast(Round(Sum(S.TWin)/Nullif(Count(Distinct S.GamingDate),0),2)as varchar) between '15'   and '39.99'   then '11'
    Else null
End as "ADT 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

1 个答案:

答案 0 :(得分:2)

您正在投射为varchar,导致您的BETWEEN按字母顺序进行比较,这就是您的结果出错的原因。删除演员陈述,如下所示:

Select S.Meta_ID as "Player ID"
,Round(Sum(S.TWin)/Nullif(Count(Distinct S.GamingDate),0),2) as ADT
,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"

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

为了解释你得到的结果,按字母顺序31.25介于2000和3499.99之间,这就是为什么你得到该行的ADT值为2的原因。