我有這樣的查詢需要的一點點幫助整理查詢
SELECT
CDV.SetId
,CDV.DateImported
,CDV.ProductName
,sum(CDV.TransferedCapital) as [TransferedCapital]
,sum(case when CA.CaseActionDefinitionId = 87 then CDV.TransferedCapital else 0 end) as [WithdrawValue]
,sum(CDV.TransferedCapital)-sum(case when CA.CaseActionDefinitionId = 87 then CDV.TransferedCapital else 0 end) as [Left]
,(sum(case when CA.CaseActionDefinitionId = 87 then CDV.TransferedCapital else 0 end)/sum(CDV.TransferedCapital)*100) as [Withdraw%]
,sum(CDV.PaymentValue) as [PaymentValue]
from
CaseDetailsView as CDV
join CaseActionHistory as CA on CA.CaseDetailId = CDV.CaseDetailsId
join PaymentCaseHistory as PCH on PCH.CaseDetailsId = CDV.CaseDetailsId
join PaymentSession as PS on PS.SessionId = PCH.SessionId
where
(CDV.ClientId = @ClientId or @ClientId IS NULL)
and
(CA.IsDeleted IS NULL or CA.IsDeleted <> 'True')
group by
CDV.SetId, CDV.DateImported, CDV.ProductName
order by
CDV.SetId
而且我得到了「小」(讀作:巨大的)問題,完成它。
我需要添加這樣的領域:
DATEPART(周,PS.SessionDate)
如果沒有這一個簡單的查詢工作完美。
加入這一個我喜歡的東西后:
SELECT
CDV.SetId
,CDV.DateImported
,CDV.ProductName
,sum(CDV.TransferedCapital) as [TransferedCapital]
,sum(case when CA.CaseActionDefinitionId = 87 then CDV.TransferedCapital else 0 end) as [WithdrawValue]
,sum(CDV.TransferedCapital)-sum(case when CA.CaseActionDefinitionId = 87 then CDV.TransferedCapital else 0 end) as [Left]
,(sum(case when CA.CaseActionDefinitionId = 87 then CDV.TransferedCapital else 0 end)/sum(CDV.TransferedCapital)*100) as [Withdraw%]
,sum(CDV.PaymentValue) as [PaymentValue]
,datepart(week,PS.SessionDate) as [SessionDate]
from
CaseDetailsView as CDV
join CaseActionHistory as CA on CA.CaseDetailId = CDV.CaseDetailsId
join PaymentCaseHistory as PCH on PCH.CaseDetailsId = CDV.CaseDetailsId
join PaymentSession as PS on PS.SessionId = PCH.SessionId
where
(CDV.ClientId = @ClientId or @ClientId IS NULL)
and
(CA.IsDeleted IS NULL or CA.IsDeleted <> 'True')
group by
CDV.SetId, CDV.DateImported, CDV.ProductName, datepart(week, PS.SessionDate)
order by
CDV.SetId
但是查詢沒有顯示所有值。當然這是因爲使用「內連接」。
但是,當我更換內與左連接加入象下面這樣:
SELECT
CDV.SetId
,CDV.DateImported
,CDV.ProductName
,sum(CDV.TransferedCapital) as [TransferedCapital]
,sum(case when CA.CaseActionDefinitionId = 87 then CDV.TransferedCapital else 0 end) as [WithdrawValue]
,sum(CDV.TransferedCapital)-sum(case when CA.CaseActionDefinitionId = 87 then CDV.TransferedCapital else 0 end) as [Left]
,(sum(case when CA.CaseActionDefinitionId = 87 then CDV.TransferedCapital else 0 end)/sum(CDV.TransferedCapital)*100) as [Withdraw%]
,sum(CDV.PaymentValue) as [PaymentValue]
,datepart(week,PS.SessionDate) as [SessionDate]
from
CaseDetailsView as CDV
join CaseActionHistory as CA on CA.CaseDetailId = CDV.CaseDetailsId
left join PaymentCaseHistory as PCH on PCH.CaseDetailsId = CDV.CaseDetailsId
left join PaymentSession as PS on PS.SessionId = PCH.SessionId
where
(CDV.ClientId = @ClientId or @ClientId IS NULL)
and
(CA.IsDeleted IS NULL or CA.IsDeleted <> 'True')
group by
CDV.SetId, CDV.DateImported, CDV.ProductName, datepart(week, PS.SessionDate)
order by
CDV.SetId
值相乘得到。
想法?
基礎上嘗試,測試等等等等我完成了某事象:
SELECT
CDV.SetId
,CDV.DateImported
,CDV.ProductName
,COALESCE(sum(CDV.TransferedCapital),0) as [TransferedCapital]
,COALESCE(sum(case when CA.CaseActionDefinitionId = 87 then CDV.TransferedCapital else 0 end),0) as [WithdrawValue]
,COALESCE(sum(CDV.TransferedCapital)-sum(case when CA.CaseActionDefinitionId = 87 then CDV.TransferedCapital else 0 end),0) as [Left]
,COALESCE((sum(case when CA.CaseActionDefinitionId = 87 then CDV.TransferedCapital else 0 end)/sum(CDV.TransferedCapital)*100),0) as [Withdraw%]
,COALESCE(sum(PCH.PaymentValue),0) as [PaymentValue]
,COALESCE(datediff(week,CDV.DateImported, PS.SessionDate),0) as [Week]
from
CaseActionHistory as CA
join CaseDetailsView as CDV on CA.CaseDetailId = CDV.CaseDetailsId
join PaymentCaseHistory as PCH on PCH.ActionArchiveId = CA.CaseActionId
join PaymentSession as PS on PS.SessionId = PCH.SessionId
where
(CDV.ClientId = @ClientId or @ClientId IS NULL)
and
(CA.IsDeleted IS NULL or CA.IsDeleted <> 'True')
and
(CDV.SetId = @SetId or @SetId IS NULL)
group by
CDV.SetId, CDV.DateImported, CDV.ProductName, datediff(week,CDV.DateImported, PS.SessionDate)
order by
CDV.SetId, datediff(week,CDV.DateImported, PS.SessionDate)
本身做工不錯查詢。我只是需要它裏面一兩件事:
的行已顯示
,COALESCE(sum(CDV.TransferedCapital),0) as [TransferedCapital]
,COALESCE(sum(case when CA.CaseActionDefinitionId = 87 then CDV.TransferedCapital else 0 end),0) as [WithdrawValue]
,COALESCE(sum(CDV.TransferedCapital)-sum(case when CA.CaseActionDefinitionId = 87 then CDV.TransferedCapital else 0 end),0) as [Left]
,COALESCE((sum(case when CA.CaseActionDefinitionId = 87 then CDV.TransferedCapital else 0 end)/sum(CDV.TransferedCapital)*100),0) as [Withdraw%]
,COALESCE(sum(PCH.PaymentValue),0) as [PaymentValue]
總和整個SETID。我想過聯盟,但它可能工作?
查詢與UNION:BY子句
SELECT
CDV.SetId
,CDV.DateImported
,CDV.ProductName
,COALESCE(sum(CDV.TransferedCapital),0) as [TransferedCapital]
,COALESCE(sum(case when CA.CaseActionDefinitionId = 87 then CDV.TransferedCapital else 0 end),0) as [WithdrawValue]
,COALESCE(sum(CDV.TransferedCapital)-sum(case when CA.CaseActionDefinitionId = 87 then CDV.TransferedCapital else 0 end),0) as [Left]
,COALESCE((sum(case when CA.CaseActionDefinitionId = 87 then CDV.TransferedCapital else 0 end)/sum(CDV.TransferedCapital)*100),0) as [Withdraw%]
,COALESCE(sum(PCH.PaymentValue),0) as [PaymentValue]
,COALESCE(datediff(week,CDV.DateImported, PS.SessionDate),0) as [Week]
from
CaseActionHistory as CA
join CaseDetailsView as CDV on CA.CaseDetailId = CDV.CaseDetailsId
join PaymentCaseHistory as PCH on PCH.ActionArchiveId = CA.CaseActionId
join PaymentSession as PS on PS.SessionId = PCH.SessionId
where
(CDV.ClientId = @ClientId or @ClientId IS NULL)
and
(CA.IsDeleted IS NULL or CA.IsDeleted <> 'True')
and
(CDV.SetId = @SetId or @SetId IS NULL)
group by
CDV.SetId, CDV.DateImported, CDV.ProductName, datediff(week,CDV.DateImported, PS.SessionDate)
UNION ALL
SELECT
CDV.SetId
,CDV.DateImported
,CDV.ProductName
,COALESCE(sum(CDV.TransferedCapital),0) as [TransferedCapital]
,COALESCE(sum(case when CA.CaseActionDefinitionId = 87 then CDV.TransferedCapital else 0 end),0) as [WithdrawValue]
,COALESCE(sum(CDV.TransferedCapital)-sum(case when CA.CaseActionDefinitionId = 87 then CDV.TransferedCapital else 0 end),0) as [Left]
,COALESCE((sum(case when CA.CaseActionDefinitionId = 87 then CDV.TransferedCapital else 0 end)/sum(CDV.TransferedCapital)*100),0) as [Withdraw%]
,COALESCE(sum(CDV.PaymentValue),0) as [PaymentValue]
,NULL
from
CaseActionHistory as CA
join CaseDetailsView as CDV on CA.CaseDetailId = CDV.CaseDetailsId
where
(CDV.ClientId = @ClientId or @ClientId IS NULL)
and
(CA.IsDeleted IS NULL or CA.IsDeleted <> 'True')
and
(CDV.SetId = @SetId or @SetId IS NULL)
group by
CDV.SetId, CDV.DateImported, CDV.ProductName
order by
CDV.SetId
你知道連接是如何工作的嗎? - http://www.codeproject.com/KB/database/Visual_SQL_Joins/Visual_SQL_JOINS_orig.jpg –
是的,我有,但仍然沒有解決我的問題。如果我可以自己管理,我不會打擾你。 – glaeran
我不明白「整個SetId」是什麼意思。你能解釋一下嗎? –