0

我有一个从多个表中提取数据的 SQL 查询(我有 11 个连接)

有一个“ARTransaction”表,其中包含费用、付款、调整等,其中有一个描述交易类型的“transactionCodeID”列。

我正在尝试选择大量数据,但需要两个单独的列(上面带有注释的列),一列用于收费,一列用于付款。有没有办法在不使用 where 子句的情况下实现这一点?我尝试使用嵌套的选择语句,但它为每一行返回相同的值(总量)

我附上下面的查询 - 在此先感谢!此外,我对数据检索还很陌生,所以如果其他任何东西看起来很奇怪,任何其他建议都非常感谢。

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
4

1 回答 1

1

您可以使用 CASE 语句来实现这一点:-

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
于 2016-11-21T20:39:21.523 回答