使用SUM在CASE语句中查询

时间:2015-07-22 12:07:44

标签: sql-server

我需要在CASE语句中使用嵌套查询。我需要使用Max(AgeDate)语句中SELECT语句中正确检索到的CASE,但我的Max(AgeDate)语句中没有看到SELECT我的CASE声明。请帮忙......

Select 
       Inv.InvoiceNumber,
       Fact_FinInvoice.InvoiceID,
       dbo.Dim_Date.FullDate AS InvoiceDate,
       pra.PracticeName,
       pra.PracticeLogonName,
       pat.AccNo,
       pat.FileNo,
       pmai.MedicalAidNumber,
       per.Title + ' ' + per.Initials + ' ' + per.Surname AS PatientName,
       sch.SCHEMES5,
       sch.SchemeOption,
(SELECT MAX(B.AgeDate) FROM Fact_FinInvoice B WHERE Fact_FinInvoice.InvoiceID = B.InvoiceID AND Fact_FinInvoice.PracticeIdKey = B.PracticeIdKey)  MaxAgeDate,

       SUM(Fact_FinInvoice.Amount) AS Amount,
       SUM(Fact_FinInvoice.AmountFunder) AS AmountFunder,
       SUM(Fact_FinInvoice.AmountPatient) AS AmountPatient,  
       SUM(CASE WHEN DATEDIFF(dd, MaxAgeDate, GETDATE()) between 0 and 29 and AmountFunder != 0 THEN AmountFunder ELSE 0 END) as FunderCurrent,
       SUM(CASE WHEN DATEDIFF(dd, MaxAgeDate, GETDATE()) between 30 and 59 and AmountFunder != 0 THEN AmountFunder ELSE 0 END) as Funder30Days, 
       SUM(CASE WHEN DATEDIFF(dd, MaxAgeDate, GETDATE()) between 60 and 89 and AmountFunder != 0 THEN AmountFunder ELSE 0 END) as Funder60Days,
       SUM(CASE WHEN DATEDIFF(dd, MaxAgeDate, GETDATE()) between 90 and 119 and AmountFunder != 0 THEN AmountFunder ELSE 0 END) as Funder90Days,
       SUM(CASE WHEN DATEDIFF(dd, MaxAgeDate, GETDATE()) >= 120 and AmountFunder != 0 THEN AmountFunder ELSE 0 END) as Funder120Days, 

       SUM(CASE WHEN DATEDIFF(dd, MaxAgeDate, GETDATE()) between 0 and 29 and AmountPatient != 0 THEN AmountPatient ELSE 0 END) as PatientCurrent,
       SUM(CASE WHEN DATEDIFF(dd, MaxAgeDate, GETDATE()) between 30 and 59 and AmountPatient != 0 THEN AmountPatient ELSE 0 END) as Patient30Days, 
       SUM(CASE WHEN DATEDIFF(dd, MaxAgeDate, GETDATE()) between 60 and 89 and AmountPatient != 0 THEN AmountPatient ELSE 0 END) as Patient60Days,
       SUM(CASE WHEN DATEDIFF(dd, MaxAgeDate, GETDATE()) between 90 and 119 and AmountPatient != 0 THEN AmountPatient ELSE 0 END) as Patient90Days,
       SUM(CASE WHEN DATEDIFF(dd, MaxAgeDate, GETDATE()) >= 120 and AmountPatient != 0 THEN AmountPatient ELSE 0 END) as Patient120Days, 

       mcs.Name AS BureauManager,
       mcso.Name AS BureauOfficer
FROM            dbo.Fact_FinInvoice LEFT OUTER JOIN
                dbo.Dim_Practice pra ON dbo.Fact_FinInvoice.PracticeIdKey = pra.PracticeIDCode LEFT OUTER JOIN
                dbo.Fact_Invoice Inv ON dbo.Fact_FinInvoice.InvoiceID = Inv.ID LEFT OUTER JOIN
                dbo.Dim_PersonMAInfo pmai  INNER JOIN
                dbo.Dim_Scheme sch  ON sch.CodeNo = pmai.CodeNo ON Inv.PersonMAInfoCode = pmai.PersonMAInfoID  LEFT OUTER JOIN
                dbo.Dim_BureauStaffProvider bsp  INNER JOIN
                dbo.Dim_MediChargeStaff mcs ON mcs.ID = bsp.BureauStaffId ON bsp.ProviderID = Inv.ProviderId INNER JOIN
                dbo.Dim_MediChargeStaff mcso ON mcs.Manager = mcso.ID LEFT OUTER JOIN
                dbo.Dim_Date ON Inv.InvoiceDateKey = dbo.Dim_Date.DateKey LEFT OUTER JOIN
                dbo.Dim_Patient pat INNER JOIN
                dbo.Dim_PersonData per ON per.PersonDataId = pat.PersonId ON pat.PatientId = Inv.PatientKey



GROUP BY    Inv.InvoiceNumber,
            Fact_FinInvoice.InvoiceID,
            dbo.Dim_Date.FullDate,
            pra.PracticeName,
            pra.PracticeLogonName,
            pmai.MedicalAidNumber,
            sch.SCHEMES5,
            sch.SchemeOption,
            mcs.Name,
            mcso.Name,
            pat.AccNo,
            pat.FileNo,
            per.Title + ' ' + per.Initials + ' ' + per.Surname,
            [Fact_FinInvoice].PracticeIdKey 

3 个答案:

答案 0 :(得分:2)

在顶部使用CTE,然后将CTE与您的连接一起加入:

;with mycte as (
select MAX(AgeDate) as MaxAgeDate,InvoiceID  FROM Fact_FinInvoice 
group by InvoiceID
)

Select 
       Inv.InvoiceNumber,
       Fact_FinInvoice.InvoiceID,
       dbo.Dim_Date.FullDate AS InvoiceDate,
       pra.PracticeName,
       pra.PracticeLogonName,
       pat.AccNo,
       pat.FileNo,
       pmai.MedicalAidNumber,
       per.Title + ' ' + per.Initials + ' ' + per.Surname AS PatientName,
       sch.SCHEMES5,
       sch.SchemeOption,
        MaxAgeDate,
       SUM(Fact_FinInvoice.Amount) AS Amount,
       SUM(Fact_FinInvoice.AmountFunder) AS AmountFunder,
       SUM(Fact_FinInvoice.AmountPatient) AS AmountPatient,  
       SUM(CASE WHEN DATEDIFF(dd, MaxAgeDate, GETDATE()) between 0 and 29 and AmountFunder != 0 THEN AmountFunder ELSE 0 END) as FunderCurrent,
       SUM(CASE WHEN DATEDIFF(dd, MaxAgeDate, GETDATE()) between 30 and 59 and AmountFunder != 0 THEN AmountFunder ELSE 0 END) as Funder30Days, 
       SUM(CASE WHEN DATEDIFF(dd, MaxAgeDate, GETDATE()) between 60 and 89 and AmountFunder != 0 THEN AmountFunder ELSE 0 END) as Funder60Days,
       SUM(CASE WHEN DATEDIFF(dd, MaxAgeDate, GETDATE()) between 90 and 119 and AmountFunder != 0 THEN AmountFunder ELSE 0 END) as Funder90Days,
       SUM(CASE WHEN DATEDIFF(dd, MaxAgeDate, GETDATE()) >= 120 and AmountFunder != 0 THEN AmountFunder ELSE 0 END) as Funder120Days, 

       SUM(CASE WHEN DATEDIFF(dd, MaxAgeDate, GETDATE()) between 0 and 29 and AmountPatient != 0 THEN AmountPatient ELSE 0 END) as PatientCurrent,
       SUM(CASE WHEN DATEDIFF(dd, MaxAgeDate, GETDATE()) between 30 and 59 and AmountPatient != 0 THEN AmountPatient ELSE 0 END) as Patient30Days, 
       SUM(CASE WHEN DATEDIFF(dd, MaxAgeDate, GETDATE()) between 60 and 89 and AmountPatient != 0 THEN AmountPatient ELSE 0 END) as Patient60Days,
       SUM(CASE WHEN DATEDIFF(dd, MaxAgeDate, GETDATE()) between 90 and 119 and AmountPatient != 0 THEN AmountPatient ELSE 0 END) as Patient90Days,
       SUM(CASE WHEN DATEDIFF(dd, MaxAgeDate, GETDATE()) >= 120 and AmountPatient != 0 THEN AmountPatient ELSE 0 END) as Patient120Days, 

       mcs.Name AS BureauManager,
       mcso.Name AS BureauOfficer
FROM            dbo.Fact_FinInvoice LEFT OUTER JOIN
                dbo.Dim_Practice pra ON dbo.Fact_FinInvoice.PracticeIdKey = pra.PracticeIDCode LEFT OUTER JOIN
                dbo.Fact_Invoice Inv ON dbo.Fact_FinInvoice.InvoiceID = Inv.ID LEFT OUTER JOIN
                dbo.Dim_PersonMAInfo pmai  INNER JOIN
                dbo.Dim_Scheme sch  ON sch.CodeNo = pmai.CodeNo ON Inv.PersonMAInfoCode = pmai.PersonMAInfoID  LEFT OUTER JOIN
                dbo.Dim_BureauStaffProvider bsp  INNER JOIN
                dbo.Dim_MediChargeStaff mcs ON mcs.ID = bsp.BureauStaffId ON bsp.ProviderID = Inv.ProviderId INNER JOIN
                dbo.Dim_MediChargeStaff mcso ON mcs.Manager = mcso.ID LEFT OUTER JOIN
                dbo.Dim_Date ON Inv.InvoiceDateKey = dbo.Dim_Date.DateKey LEFT OUTER JOIN
                dbo.Dim_Patient pat INNER JOIN
                dbo.Dim_PersonData per ON per.PersonDataId = pat.PersonId ON pat.PatientId = Inv.PatientKey
                join mycte ct
                on Fact_FinInvoice.InvoiceID = ct.InvoiceID

GROUP BY    Inv.InvoiceNumber,
            Fact_FinInvoice.InvoiceID,
            dbo.Dim_Date.FullDate,
            pra.PracticeName,
            pra.PracticeLogonName,
            pmai.MedicalAidNumber,
            sch.SCHEMES5,
            sch.SchemeOption,
            mcs.Name,
            mcso.Name,
            pat.AccNo,
            pat.FileNo,
            per.Title + ' ' + per.Initials + ' ' + per.Surname,
            [Fact_FinInvoice].PracticeIdKey

答案 1 :(得分:0)

您不能在select中的其他位置使用select部分中给出的别名,但您可以使用外部apply来获取您将在select中使用的数据,如下所示:

select
    ....
    sch.SchemeOption,
    AD.MaxAgeDate,
    ....
    SUM(CASE WHEN DATEDIFF(dd, AD.MaxAgeDate, GETDATE())
from
    dbo.Fact_FinInvoice
    outer apply (
        SELECT MAX(B.AgeDate) 
        FROM Fact_FinInvoice B 
        WHERE Fact_FinInvoice.InvoiceID = B.InvoiceID AND
        Fact_FinInvoice.PracticeIdKey = B.PracticeIdKey)  AD
    LEFT OUTER JOIN ...

并且您还可以将外部应用中的datediff计算为第二列,因此您不必多次重复相同的代码。

答案 2 :(得分:0)

使用交叉申请

SELECT          Inv.InvoiceNumber,
                Fact_FinInvoice.InvoiceID,
                dbo.Dim_Date.FullDate                              AS InvoiceDate,
                pra.PracticeName,
                pra.PracticeLogonName,
                pat.AccNo,
                pat.FileNo,
                pmai.MedicalAidNumber,
                per.Title + ' ' + per.Initials + ' ' + per.Surname AS PatientName,
                sch.SCHEMES5,
                sch.SchemeOption,
                cs.MaxAgeDate,
                Sum(Fact_FinInvoice.Amount)                        AS Amount,
                Sum(Fact_FinInvoice.AmountFunder)                  AS AmountFunder,
                Sum(Fact_FinInvoice.AmountPatient)                 AS AmountPatient,
                Sum(CASE
                      WHEN Datediff(dd, MaxAgeDate, Getdate()) BETWEEN 0 AND 29
                           AND AmountFunder != 0 THEN AmountFunder
                      ELSE 0
                    END)                                           AS FunderCurrent,
                Sum(CASE
                      WHEN Datediff(dd, MaxAgeDate, Getdate()) BETWEEN 30 AND 59
                           AND AmountFunder != 0 THEN AmountFunder
                      ELSE 0
                    END)                                           AS Funder30Days,
                Sum(CASE
                      WHEN Datediff(dd, MaxAgeDate, Getdate()) BETWEEN 60 AND 89
                           AND AmountFunder != 0 THEN AmountFunder
                      ELSE 0
                    END)                                           AS Funder60Days,
                Sum(CASE
                      WHEN Datediff(dd, MaxAgeDate, Getdate()) BETWEEN 90 AND 119
                           AND AmountFunder != 0 THEN AmountFunder
                      ELSE 0
                    END)                                           AS Funder90Days,
                Sum(CASE
                      WHEN Datediff(dd, MaxAgeDate, Getdate()) >= 120
                           AND AmountFunder != 0 THEN AmountFunder
                      ELSE 0
                    END)                                           AS Funder120Days,
                Sum(CASE
                      WHEN Datediff(dd, MaxAgeDate, Getdate()) BETWEEN 0 AND 29
                           AND AmountPatient != 0 THEN AmountPatient
                      ELSE 0
                    END)                                           AS PatientCurrent,
                Sum(CASE
                      WHEN Datediff(dd, MaxAgeDate, Getdate()) BETWEEN 30 AND 59
                           AND AmountPatient != 0 THEN AmountPatient
                      ELSE 0
                    END)                                           AS Patient30Days,
                Sum(CASE
                      WHEN Datediff(dd, MaxAgeDate, Getdate()) BETWEEN 60 AND 89
                           AND AmountPatient != 0 THEN AmountPatient
                      ELSE 0
                    END)                                           AS Patient60Days,
                Sum(CASE
                      WHEN Datediff(dd, MaxAgeDate, Getdate()) BETWEEN 90 AND 119
                           AND AmountPatient != 0 THEN AmountPatient
                      ELSE 0
                    END)                                           AS Patient90Days,
                Sum(CASE
                      WHEN Datediff(dd, MaxAgeDate, Getdate()) >= 120
                           AND AmountPatient != 0 THEN AmountPatient
                      ELSE 0
                    END)                                           AS Patient120Days,
                mcs.Name                                           AS BureauManager,
                mcso.Name                                          AS BureauOfficer
FROM            dbo.Fact_FinInvoice FFI
LEFT OUTER JOIN dbo.Dim_Practice pra ON FFI.PracticeIdKey = pra.PracticeIDCode
LEFT OUTER JOIN dbo.Fact_Invoice Inv ON FFI.InvoiceID = Inv.ID
LEFT OUTER JOIN dbo.Dim_PersonMAInfo pmai
                INNER JOIN dbo.Dim_Scheme sch ON sch.CodeNo = pmai.CodeNo ON Inv.PersonMAInfoCode = pmai.PersonMAInfoID
LEFT OUTER JOIN dbo.Dim_BureauStaffProvider bsp
                INNER JOIN dbo.Dim_MediChargeStaff mcs ON mcs.ID = bsp.BureauStaffId ON bsp.ProviderID = Inv.ProviderId
INNER JOIN      dbo.Dim_MediChargeStaff mcso ON mcs.Manager = mcso.ID
LEFT OUTER JOIN dbo.Dim_Date ON Inv.InvoiceDateKey = dbo.Dim_Date.DateKey
LEFT OUTER JOIN dbo.Dim_Patient pat
                INNER JOIN dbo.Dim_PersonData per ON per.PersonDataId = pat.PersonId ON pat.PatientId = Inv.PatientKey
CROSS APPLY     (SELECT Max(B.AgeDate) AS MaxAgeDAte
                 FROM   Fact_FinInvoice B
                 WHERE  FFI.InvoiceID = B.InvoiceID
                        AND FFI.PracticeIdKey = B.PracticeIdKey) CS
GROUP           BY Inv.InvoiceNumber,
                   FFI.InvoiceID,
                   dbo.Dim_Date.FullDate,
                   pra.PracticeName,
                   pra.PracticeLogonName,
                   pmai.MedicalAidNumber,
                   sch.SCHEMES5,
                   sch.SchemeOption,
                   mcs.Name,
                   mcso.Name,
                   pat.AccNo,
                   pat.FileNo,
                   per.Title + ' ' + per.Initials + ' ' + per.Surname,
                   [FFI].PracticeIdKey