2014-09-26 100 views
0

我想創建一個基於來自其他幾列的信息計算的列,這對我來說很難作爲SQL的新人。下面是我嘗試使用的代碼,但它對「Sum(SlotActual + TableActual)As Actual」這一行感到憤怒。我猜這是由於這些是別名。我想我需要使用子查詢,但我不確定,因爲我以前沒有這樣做過。如果可能的話,我寧願不要顯示SlotActual或TableActual,我只需要計算這些,然後添加它們以獲得實際值。這是在SQL 2008中完成的。請讓我知道我是否遺漏了您需要的任何信息。複雜的添加問題

我越來越
Select Ltrim(Rtrim(P.Player_ID)) as "Player ID", 
Ltrim(Rtrim(P.FirstName)) as "First Name", 
Ltrim(Rtrim(P.LastName)) as "Last Name", 
Sum(CashIn) as "Cash In", 
Sum(CashOut) as "Cash Out", 
Sum(Jackpot) as "Jackpot", 
Sum(case when S.StatType = 'Slot' then S.CashIn - S.CashOut - S.JackPot end) as SlotActual, 
Sum(case when S.StatType = 'Pit' then S.CashIn + S.ChipsIn + S.FrontIn + S.CreditIn - S.CashOut end) as TableActual, 
Sum(SlotActual + TableActual) As Actual 

From dbo.CDS_PLAYER as P 
Join dbo.CDS_ACCOUNT as A 
    On P.Player_ID = A.Primary_ID 
Join dbo.CDS_STATDAY as S 
    On P.Player_ID = S.Meta_ID 

Where S.GamingDate Between '09/15/2014 12:00:00 AM' and '09/21/2014 11:59:59 PM' 
And P.EntryDate Between '09/15/2014 12:00:00 AM' and '09/21/2014 11:59:59 PM' 
And S.IDType = 'P' 

Group by Ltrim(Rtrim(P.Player_ID)) , 
Ltrim(Rtrim(P.FirstName)), 
Ltrim(Rtrim(P.LastName)) 

錯誤信息如下:

消息207,級別16,狀態3,行3 無效的列名稱SlotActual「。 消息207,級別16,狀態3,行3 無效的列名稱'TableActual'。

+0

請張貼錯誤的SQL是給你的。 – 2014-09-26 17:58:31

+0

您不能在集合函數中使用別名。 sql服務器不支持這個。嘗試把你的整個公式扔在總和中。 – paqogomez 2014-09-26 18:02:22

回答

1

好的,我現在看到了問題。你不能像這樣引用一個選擇的命名字段。

替換:

Sum(SlotActual + TableActual) As Actual 

而是執行此操作:

(Sum(case when S.StatType = 'Slot' then S.CashIn - S.CashOut - S.JackPot end) + 
Sum(case when S.StatType = 'Pit' then S.CashIn + S.ChipsIn + S.FrontIn + S.CreditIn - S.CashOut end)) As Actual 

要允許空值:

(Sum(ISNULL(case when S.StatType = 'Slot' then S.CashIn - S.CashOut - S.JackPot end), 0) + 
Sum(ISNULL(case when S.StatType = 'Pit' then S.CashIn + S.ChipsIn + S.FrontIn + S.CreditIn - S.CashOut end), 0)) As Actual 

ISNULL允許當左參數是要使用的值空值。

如果您的數據允許您也可以爲CASE添加一個其他值,S不會有任何值。

(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)) As Actual 
+0

好吧,這是有道理的,並得到真正接近我需要的東西,但有一個我忘記提及的漁獲。有時槽或坑將爲空。當我嘗試這種變化,如果他們中的一個是null整個回來返回null是否有一種方法來錯誤捕獲null並將其變爲0或忽略它? – 2014-09-26 18:05:59

+0

用'ISNULL'或'Coalesce'將它包裹起來, – paqogomez 2014-09-26 18:07:00

+0

更新了我的文章,以表達null關注。 – 2014-09-26 18:10:45

0

使用的包裝是這樣的:

select X.*, Sum(ISNULL(SlotActual,0) + ISNULL(TableActual,0)) As Actual 
From (
Select Ltrim(Rtrim(P.Player_ID)) as "Player ID", 
Ltrim(Rtrim(P.FirstName)) as "First Name", 
Ltrim(Rtrim(P.LastName)) as "Last Name", 
Sum(CashIn) as "Cash In", 
Sum(CashOut) as "Cash Out", 
Sum(Jackpot) as "Jackpot", 
Sum(case when S.StatType = 'Slot' then S.CashIn - S.CashOut - S.JackPot end) as SlotActual, 
Sum(case when S.StatType = 'Pit' then S.CashIn + S.ChipsIn + S.FrontIn + S.CreditIn - S.CashOut end) as TableActual, 

From dbo.CDS_PLAYER as P 
Join dbo.CDS_ACCOUNT as A 
    On P.Player_ID = A.Primary_ID 
Join dbo.CDS_STATDAY as S 
    On P.Player_ID = S.Meta_ID 

Where S.GamingDate Between '09/15/2014 12:00:00 AM' and '09/21/2014 11:59:59 PM' 
And P.EntryDate Between '09/15/2014 12:00:00 AM' and '09/21/2014 11:59:59 PM' 
And S.IDType = 'P' 

Group by Ltrim(Rtrim(P.Player_ID)) , 
Ltrim(Rtrim(P.FirstName)), 
Ltrim(Rtrim(P.LastName)) 
) X