下面是我遇到的問題。一切工作很好,直到我添加了「亞洲」的線。這個信息必須來自一個新表,所以我不得不做一個新的連接。一旦我做到了這一點,我開始對這些信息產生問題。問題是CDS_MailPlayer可以爲同一個人擁有多個Mail_ID。然後,它會爲每個Mail_ID執行所有Sum,然後將它們一起添加。因此,如果玩家顯示100美元的實際值,但有2個郵件編號,他們的實際值將以200美元出現。如果他們有10個電子郵件ID,那麼他們的實際價值將猛漲至1000美元。數據總結多次
我該如何添加一張支票來查看某人是否有161到166之間的郵件ID,而不會搞亂我的數學?
此外,還有一個方法可以修改Where子句以將滾動條拉長12個月,如果您在2014年12月的任何一天拉取報表,它將拉動12/1/2013-11/30/2014但如果您在2015年1月提交報告,則日期範圍將更改爲2014年1月1日/ 2014年1月1日。我儘可能接近知道如何知道。
和往常一樣,關於如何清理代碼並使其更快/更好的任何建議都將被讚賞,對於SQL來說還是一個新的東西。
使用SQL Server 2000中
Select P.Player_ID as "Player ID"
,Ltrim(RTrim(P.FirstName)) as "First Name"
,Ltrim(RTrim(P.LastName)) as "Last Name"
,Convert(char(10),cast(P.Birthday as datetime),101) as "Birthday"
,P.EMail as "E-Mail"
,Ltrim(RTrim(A.City1)) as "City"
,Ltrim(RTrim(A.State1)) as"State"
,Ltrim(RTrim(A.Zip1)) as "Zip"
,A.BadAddress1
,A.CustomFlag3 as "Do Not Mail"
,P.LL_Dap_Flag
,Round(Sum(S.TWin),2) as Theo
,Round(((Sum(case when S.StatType = 'Slot' then S.CashIn - S.CashOut - S.JackPot Else 0 end)*.75)+
(Sum(case when S.StatType = 'Pit' then S.CashIn + S.CreditIn + S.ChipsIn + S.FrontIn - S.CashOut Else 0 end)*.4)),2) as Actual
,Count(Distinct(S.GamingDate)) as Trips
,Count(Distinct Month(S.GamingDate)) as Month
,Count(Distinct case when S.StatType = 'Slot' then S.GamingDate end) as "Slot Trips"
,Count(Distinct case when S.StatType = 'Slot' then Month(S.GamingDate) end)as "Slot Month"
,Count(Distinct case when S.StatType = 'Pit' then S.GamingDate end) as "Table Trips"
,Count(Distinct case when S.StatType = 'Pit' then Month(S.GamingDate) end)as "Table Month"
,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)*.75)+
(Sum(case when S.StatType = 'Pit' then S.CashIn + S.CreditIn + S.ChipsIn + S.FrontIn - S.CashOut Else 0 end)*.4))/Nullif(Count(Distinct(S.GamingDate)),0)),2) as ADL
,Round((((Sum(case when S.StatType = 'Slot' then S.CashIn - S.CashOut - S.JackPot Else 0 end)*.75)+
(Sum(case when S.StatType = 'Pit' then S.CashIn + S.CreditIn + S.ChipsIn + S.FrontIn - S.CashOut Else 0 end)*.4))/Nullif(Count(Distinct Month (S.GamingDate)),0)),2) as AML
,case when Sum(case when S.StatType = 'Pit' then S.TWin Else 0 end)>=(Sum(S.TWin)*.8)then 'Table' else 'Slot' end as "Slot/Table"
,case when Sum(case when M.Mail_ID between '161' and '166' then 1 else 0 end)>0 Then 'Y' else 'N' end as "Asian"
,Case
When R.Rank_ID = '1' then 'Silver'
When R.Rank_ID = '2' then 'Gold'
When R.Rank_ID = '3' then 'Platinum'
When R.Rank_ID = '4' then 'Diamond'
Else 'Copper'
end as "Rank"
,P.HostUser_ID
,P.Referral
From dbo.CDS_ACCOUNT as A
Join dbo.CDS_PLAYER as P
on A.[Primary_ID] = P.[Player_ID]
Join dbo.CDS_STATDAY as S
on A.[Primary_ID] = S.[Meta_ID]
Join dbo.CDS_MAILPLAYER as M
on A.[Primary_ID] = M.Player_ID
Join dbo.Tiered_Rank_Player as R
on A.[Primary_ID] = R.Player_ID
Where S.GamingDate >= Dateadd(year,-1,getdate())
And S.IDType = 'P'
And S.StatType <> 'Poker'
And A.CustomFlag1 = 'N'
And A.CustomFlag2 = 'N'
And A.CustomFlag4 = 'N'
And A.CustomFlag5 = 'N'
Group by P.Player_ID
,P.FirstName
,P.LastName
,P.Birthday
,P.EMail
,A.City1
,A.State1
,A.Zip1
,A.BadAddress1
,A.CustomFlag3
,P.LL_Dap_Flag
,R.Rank_ID
,P.HostUser_ID
,P.Referral
我的解決方案是低於我只是改了行亞洲的子查詢,然後刪除相關聯接和組從來自select語句。
Select P.Player_ID as "Player ID"
,Ltrim(RTrim(P.FirstName)) as "First Name"
,Ltrim(RTrim(P.LastName)) as "Last Name"
,Convert(char(10),cast(P.Birthday as datetime),101) as "Birthday"
,P.EMail as "E-Mail"
,Ltrim(RTrim(A.City1)) as "City"
,Ltrim(RTrim(A.State1)) as"State"
,Ltrim(RTrim(A.Zip1)) as "Zip"
,A.BadAddress1
,A.CustomFlag3 as "Do Not Mail"
,P.LL_Dap_Flag
,Round(Sum(S.TWin),2) as Theo
,Round(((Sum(case when S.StatType = 'Slot' then S.CashIn - S.CashOut - S.JackPot Else 0 end)*.75)+
(Sum(case when S.StatType = 'Pit' then S.CashIn + S.CreditIn + S.ChipsIn + S.FrontIn - S.CashOut Else 0 end)*.4)),2) as Actual
,Count(Distinct(S.GamingDate)) as Trips
,Count(Distinct Month(S.GamingDate)) as Month
,Count(Distinct case when S.StatType = 'Slot' then S.GamingDate end) as "Slot Trips"
,Count(Distinct case when S.StatType = 'Slot' then Month(S.GamingDate) end)as "Slot Month"
,Count(Distinct case when S.StatType = 'Pit' then S.GamingDate end) as "Table Trips"
,Count(Distinct case when S.StatType = 'Pit' then Month(S.GamingDate) end)as "Table Month"
,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)*.75)+
(Sum(case when S.StatType = 'Pit' then S.CashIn + S.CreditIn + S.ChipsIn + S.FrontIn - S.CashOut Else 0 end)*.4))/Nullif(Count(Distinct(S.GamingDate)),0)),2) as ADL
,Round((((Sum(case when S.StatType = 'Slot' then S.CashIn - S.CashOut - S.JackPot Else 0 end)*.75)+
(Sum(case when S.StatType = 'Pit' then S.CashIn + S.CreditIn + S.ChipsIn + S.FrontIn - S.CashOut Else 0 end)*.4))/Nullif(Count(Distinct Month (S.GamingDate)),0)),2) as AML
,case when Sum(case when S.StatType = 'Pit' then S.TWin Else 0 end)>=(Sum(S.TWin)*.8)then 'Table' else 'Slot' end as "Slot/Table"
,(Select case when Sum(case when M.Mail_ID between '161' and '166' then 1 else 0 end)>0 Then 'Y' else 'N' end as "Asian"
From dbo.CDS_MAILPLAYER as M) as "Asian"
,Case
When R.Rank_ID = '1' then 'Silver'
When R.Rank_ID = '2' then 'Gold'
When R.Rank_ID = '3' then 'Platinum'
When R.Rank_ID = '4' then 'Diamond'
Else 'Copper'
end as "Rank"
,P.HostUser_ID
,P.Referral
From dbo.CDS_ACCOUNT as A
Join dbo.CDS_PLAYER as P
on A.[Primary_ID] = P.[Player_ID]
Join dbo.CDS_STATDAY as S
on A.[Primary_ID] = S.[Meta_ID]
Join dbo.Tiered_Rank_Player as R
on A.[Primary_ID] = R.Player_ID
Where S.GamingDate between '12/1/2013' and '11/30/2014'
And S.IDType = 'P'
And S.StatType <> 'Poker'
And A.CustomFlag1 = 'N'
And A.CustomFlag2 = 'N'
And A.CustomFlag4 = 'N'
And A.CustomFlag5 = 'N'
Group by P.Player_ID
,P.FirstName
,P.LastName
,P.Birthday
,P.EMail
,A.City1
,A.State1
,A.Zip1
,A.BadAddress1
,A.CustomFlag3
,P.LL_Dap_Flag
,R.Rank_ID
,P.HostUser_ID
,P.Referral
,P.Casino_ID
謝謝你的迴應,雖然它確實出現了這樣做,但我發現了一種不同的方式來做到這一點,這對我的大腦更有意義。 :)我會用我所做的更新我的文章。 – 2014-12-11 22:31:37