Search code examples
sqlsql-servert-sqlgroup-bynested-select

SQL - charges and payments in same column; how to break it out


I have a SQL query that is pulling data from multiple tables (I have 11 joins)

There is an "ARTransaction" table which contains charges, payments, adjustments, etc. and there is a "transactionCodeID" column inside of that which describes the type of transaction.

I am trying to select a lot of data, but need two separate columns(the ones with comments above them), one for charges and one for payments. Is there a way to achieve this without using the where clause? I tried to use a nested select statement but it returned the same value for every single row (the total amount)

I am attaching the query below - thanks in advance! Also I am fairly new to data retrieval so if anything else looks wonky any other advice is greatly appreciated.

SELECT CONVERT(varchar(10),sl.ServiceDtFrom, 101) AS 'srvdate'
     , f.Alias AS 'svc dprtmnt'
     , CASE WHEN pc.Alias IS NULL
            THEN po.Name
            ELSE pc.Description END AS 'svc dept grp'
     , COUNT(clm.ID) AS 'clm cnt'
     , COUNT(p.ID) AS 'ptnt count'
     /* 1 */ 
     , SUM(ar.Amount) AS 'all chgs' --ONLY CHARGES (tt.ID IN(1,2))
     , SUM(sl.Units + sl.TimeUnits + sl.PhysicalStatusUnits) AS 'chg units sum'
     /* 2 */ 
     , SUM(ar.Amount) AS 'net pmt'  --ONLY PAYMENTS (tt.ID IN(3,4,9,10,11,12,20,21))
FROM ARTransaction ar           WITH (NOLOCK)
LEFT JOIN ServiceLine sl        WITH (NOLOCK)
    ON ar.ServiceLineID = sl.ID
LEFT JOIN Incident i            WITH (NOLOCK)
    ON sl.IncidentID = i.ID
LEFT JOIN HealthCareFacility f  WITH (NOLOCK)
    ON i.FacilityID = f.ID
LEFT JOIN ProvOrgFacility poc   WITH (NOLOCK)
    ON poc.FacilityID = f.ID
LEFT JOIN ProfitCenter pc       WITH (NOLOCK)
    ON poc.ProfitCenterID = pc.ID
LEFT JOIN ProviderOrganization po WITH (NOLOCK)
    ON i.ProvOrgID = po.ID
LEFT JOIN Claim clm             WITH (NOLOCK)
    ON i.ID = clm.IncidentID
LEFT JOIN Person p              WITH (NOLOCK)
    ON i.PatientID = p.ID
LEFT JOIN TransactionCode tc    WITH (NOLOCK)
    ON ar.TransactionCodeID = tc.ID
LEFT JOIN TransactionType tt    WITH (NOLOCK)
    ON tc.TransactionTypeID = tt.ID

WHERE i.IsReversed <> 1
      AND sl.ServiceDtFrom IS NOT NULL

GROUP BY
      sl.ServiceDtFrom, f.Alias
    , po.Name, pc.Alias, pc.Description

ORDER BY 1,3,2

Solution

  • You can use CASE statements to achieve this:-

    SELECT CONVERT(varchar(10),sl.ServiceDtFrom, 101) AS 'srvdate'
         , f.Alias AS 'svc dprtmnt'
         , CASE WHEN pc.Alias IS NULL
                THEN po.Name
                ELSE pc.Description END AS 'svc dept grp'
         , COUNT(clm.ID) AS 'clm cnt'
         , COUNT(p.ID) AS 'ptnt count'
         /* 1 */ 
         , SUM(case when tt.ID IN(1,2) then ar.Amount else 0 end) AS 'all chgs' --ONLY CHARGES (tt.ID IN(1,2))
         , SUM(sl.Units + sl.TimeUnits + sl.PhysicalStatusUnits) AS 'chg units sum'
         /* 2 */ 
         , SUM(case when tt.ID IN(3,4,9,10,11,12,20,21) then ar.Amount else 0 end) AS 'net pmt'  --ONLY PAYMENTS (tt.ID IN(3,4,9,10,11,12,20,21))
    FROM ARTransaction ar           WITH (NOLOCK)
    LEFT JOIN ServiceLine sl        WITH (NOLOCK)
        ON ar.ServiceLineID = sl.ID
    LEFT JOIN Incident i            WITH (NOLOCK)
        ON sl.IncidentID = i.ID
    LEFT JOIN HealthCareFacility f  WITH (NOLOCK)
        ON i.FacilityID = f.ID
    LEFT JOIN ProvOrgFacility poc   WITH (NOLOCK)
        ON poc.FacilityID = f.ID
    LEFT JOIN ProfitCenter pc       WITH (NOLOCK)
        ON poc.ProfitCenterID = pc.ID
    LEFT JOIN ProviderOrganization po WITH (NOLOCK)
        ON i.ProvOrgID = po.ID
    LEFT JOIN Claim clm             WITH (NOLOCK)
        ON i.ID = clm.IncidentID
    LEFT JOIN Person p              WITH (NOLOCK)
        ON i.PatientID = p.ID
    LEFT JOIN TransactionCode tc    WITH (NOLOCK)
        ON ar.TransactionCodeID = tc.ID
    LEFT JOIN TransactionType tt    WITH (NOLOCK)
        ON tc.TransactionTypeID = tt.ID
    
    WHERE i.IsReversed <> 1
          AND sl.ServiceDtFrom IS NOT NULL
    
    GROUP BY