【问题标题】:Is money value between X and Y?X和Y之间的货币价值吗?
【发布时间】:2014-10-01 17:12:28
【问题描述】:

这是一个在 Excel 中使用近似匹配的 vlookup 的简单过程,但由于某种原因,我无法使其与 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

【问题讨论】:

  • 你能给出 CDS_STATDAY 表中的示例数据,还有哪个 RDBMS?
  • 你为什么要转换成 varchar 来做数值比较?
  • 您的问题是尝试比较转换为字符串的数字,只需使用数值。另外,在转换时总是指定一个大小,只使用“varchar”是非常懒惰的。您知道仅使用“varchar”时的默认大小是多少吗?你的数据适合吗?添加大小不需要太多:varchar(25).

标签: sql case currency


【解决方案1】:

您正在转换为 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。

【讨论】:

  • 为了完整起见,您还应该将参数之间的参数更改为数字,例如 between 3500 and 1000000 而不是 between '3500' and '1000000'
  • 非常感谢,我没有意识到我可以省略数字周围的 '' 我对 SQL 仍然很陌生,它很快就解决了这个问题。现在编写代码来为需要像这样分层的其他三个项目执行此操作,并弄清楚如何找到 4 个中最好的层。对我来说很有趣:)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-11-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-08-01
相关资源
最近更新 更多