在完成查询时需要一些帮助

时间:2013-11-25 12:35:33

标签: sql-server tsql reporting-services

我有这样的查询

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

我完成它时遇到“小”(读作:巨大)问题。

我需要添加这样的字段:

日期部分(周,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但可能有用吗?


使用UNION查询:

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

3 个答案:

答案 0 :(得分:0)

逐条更改:

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

答案 1 :(得分: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;

答案 2 :(得分:0)

查看查询,您似乎没有在新的SessionDate字段中的任何地方使用PaymentCaseHistory(PCH)或PaymentSession(PS)。

所以最明显的第一个结论是,上面的这两个表包含了CaseDetailsView和CaseActionHistory每个先前结果的大量不同的周数。

您可以通过将[strong> datepart(week,PS.SessionDate)替换为[SessionDate]和MAX(DATEPART(WEEK,PS.SessionDate))来尝试这一点,看看会发生什么。此外,要按其分组,您需要在GROUP BY子句中添加相同的条件。 IE:

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