2014-10-01 103 views
0

這是一個簡單的過程在excel中使用vlookup與近似匹配,但由於某些原因,我無法使其與SQl一起工作,我確信我正在以這種方式執行此任何幫助,我們將不勝感激。這是我得到的結果的一個樣本:X和Y之間的貨幣價值?

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 
+0

你可以提供來自CDS_STATDAY表的樣本數據,還有哪些RDBMS? – radar 2014-10-01 17:17:44

+1

你爲什麼要轉換爲varchar做一個數字比較? – 2014-10-01 17:19:07

+0

你的問題是試圖比較轉換爲字符串的數字,只是使用數字值。此外,轉換時總是指定一個大小,只是使用「varchar」非常懶。當只使用「varchar」時,你知道默認大小是什麼嗎?你的數據會適合它嗎?添加大小並不需要太多:'varchar(25)'。 – 2014-10-01 18:49:36

回答

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。

+3

爲了完整性,您還應該將參數之間的參數更改爲數字,例如'3500到1000000'之間,而不是'3500'和'1000000'之間。 – 2014-10-01 17:54:42

+0

好點,我更新了答案 – SS781 2014-10-01 18:41:31

+0

非常感謝你,我沒有意識到我可以忽略數字我仍然對SQL很新,很快就解決了這個問題。現在編寫代碼來完成其他三項需要像這樣分層的項目,並找出如何找到最適合我的4層次的樂趣:) – 2014-10-01 19:44:20