2013-11-25 47 views
0

我有這樣的查詢需要的一點點幫助整理查詢

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

你知道連接是如何工作的嗎? - http://www.codeproject.com/KB/database/Visual_SQL_Joins/Visual_SQL_JOINS_orig.jpg –

+0

是的,我有,但仍然沒有解決我的問題。如果我可以自己管理,我不會打擾你。 – glaeran

+0

我不明白「整個SetId」是什麼意思。你能解釋一下嗎? –

回答

0

更改組:

group by CDV.SetId, CDV.DateImported, CDV.ProductName, PS.SessionDate 
+0

沒有。不會改變任何東西。數值仍然乘以相同的星期不計爲1,因此例如我有相同的SetId 36個星期的3條記錄 – glaeran

+0

您可以發佈有關您的ERD的圖像? –

+0

不能遺憾。但是我可以告訴你,查詢中使用的關係是唯一可以使用的關係。 – glaeran

0

你可以試試這個?

我無法嘗試這個,因爲我沒有SQLServer在工作。

select TBL1.* 
     ,(TBL1.TRANSFEREDCAPITAL + TBL1.WITHDRAWVALUE + TBL1.left + TBL1.WITHDRAW% + TBL1.PAYMENTVALUE) as [TOTALSUM] 
from (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)) as TBL1; 
+0

列PAYMENTSESSION.SessionDate在選擇列表中無效,因爲它不包含在聚合函數或GROUP BY子句中+列的名稱無效SessionDate +無法連接CDV.SetId – glaeran

+0

關於最後一次SessionDate的課程「order by 」。請現在測試。 –

+0

「TBL1.SessionDate無效」,但我剛刪除了子查詢中的空格,但它仍然要求所有字段都在主查詢中,以便查詢結果與我的相同(乘以值:)) – glaeran

0

看看查詢,您似乎沒有在您的新SessionDate字段中的任何位置使用PaymentCaseHistory(PCH)或PaymentSession(PS)。

所以最明顯的第一個結論是,上面那兩個表包含來自CaseDetailsView和CaseActionHistory的每個先前結果的許多不同的星期數。

你可以用MAX(DATEPART(WEEK,PS.SessionDate))將你的datepart(week,PS.SessionDate)替換爲[SessionDate]來試試看看會發生什麼。另外,要按它分組,您需要在GROUP BY子句中添加相同的條件。IE:

SELECT .... datepart(week,PS.SessionDate) as [SessionDate] 
FROM .... 
GROUP BY ... , datepart(week,PS.SessionDate) 
+0

不幸的是,這不是真的爲什麼我想要..值仍然不同於預期。我想它與「連接」子句相關。更重要的是(如果它會改變什麼)我發現有一種方法可以使用'ArchiveId'列將CaseActionHistory與PaymentSession表連接起來。 – glaeran

+0

更新後的第一篇文章,其他信息。請看看。 – glaeran