在行内选择语句中使用聚合函数

时间:2016-02-16 15:02:26

标签: sql sql-server tsql

T-SQL我将服务表加入付款表。我正在使用MAX,AVG for payments_rec'd和allowed_ amount。我需要PayApp表中每个服务的最后一行号码的余额。有时会列出付款然后减去付款,因此MIN余额不起作用。我正在考虑内联select语句但不能使用聚合函数内联。

SELECT DISTINCT [CDCLSVC].[RECNUM]       
,[CDCLSVC].[ID]       
,[CDCLSVC].[CLIENT_ID]       
,[CDCLIENT].[CASE_NUM]       
,[CDCLSVC].[UNIT_ID]       
,[CDCLSVC].[SUB_UNIT_ID]      
,[CDCLSVC].[SVC_ID]       
,[CDCLSVC].[EMP_ID]       
,[CDCLSVC].[BEG_DATE]       
,UPPER([CAEMP].[SORT_NAME]) as Employee       
,ISNULL([CDCLSVC].[EXTENDED_PRICE],0)  As ExtendedPrice       
,ISNULL([CDCLSVC].[BALANCE],0) As Balance         
,ISNULL(MAX([CDPAYAPP].[ALLOWED_AMT]),0) AS AllowedAmount
,ISNULL(MAX([CDPAYAPP].[ADJ_AMT]),0)As AdjustedAmount  
,ISNULL(SUM([CDPAYAPP].[PAY_APPLIED_AMT]),0)As PaymentApplied
,ISNULL(MAX([CDPAYAPP].[WRITE_OFF_AMT]),0)AS WriteOff  
,ISNULL(MIN([CDPAYAPP].[SVC_BALANCE]),0) AS SvcBalance  
,UPPER([CDPLACE].[DESC]) as Location    

FROM [AnaDEV].[dbo].[CDCLSVC]    
INNER JOIN [CAEMP]   ON [CDCLSVC].[EMP_ID] = [CAEMP].[ID]    
INNER JOIN [CDCLIENT]    ON [CDCLSVC].[CLIENT_ID] = [CDCLIENT].[ID]    
INNER JOIN [CDPLACE]   ON [CDCLSVC].[PLACE_ID] = [CDPLACE].[ID]    
INNER JOIN [CDPAYAPP]   ON [CDPAYAPP].[CLSVC_ID] = [CDCLSVC].[ID]   

WHERE [CDCLSVC].[BEG_DATE] between '2015-07-01' and DATEADD(DAY,- 
    (DATEPART(DAY, getdate())),GETDATE())

GROUP BY CDCLSVC.RECNUM,CDCLSVC.ID,CDCLSVC.CLIENT_ID,  
        CDCLIENT.CASE_NUM,CDCLSVC.UNIT_ID, CDCLSVC.SUB_UNIT_ID,
        CDCLSVC.SVC_ID,CDCLSVC.EMP_ID, CDCLSVC.BEG_DATE,    CAEMP.SORT_NAME,
        CDCLSVC.EXTENDED_PRICE,CDCLSVC.BALANCE, [CDPLACE].[DESC]   

ORDER BY [CDCLIENT].[CASE_NUM], [CDCLSVC].[ID] 

1 个答案:

答案 0 :(得分:0)

我有一个小型医疗诊所的服务表(CDCLSVC)。每项服务都有许多付款/调整(PAYAPP)。所以我有一对多的关系。我还有一个Clinicians的查找表和另一个客户的查找表。目标是通过Clinician,然后客户对服务进行分组,每个服务有一行。这很容易使用SUM,MIN,MAX聚合PAYAPP表。问题是PAYAPP表中的Balance就像支票簿,我只需要最后一个条目。

PAYAPP
ROWNUM  SVC    PRICE  ADJUSTMENT WRITE-OFF  PAYMENTS  BALANCE
1        001    $100       $25                           $75
2        001    $100                           $10       $65
3        001    $100                           $50       $15
4        001    $100                $15                  $0

每个服务最大(价格)和(调整)最大值(写入)和(付款)

问题是获得最终的BALANCE,我在左外连接后通过子查询完成了。

SELECT CDCLSVC.ID AS SERVICE_ID
      --,CDPAYAPP.[ID] AS CDPAYAPP_ID
      ,CDCLSVC.[CLIENT_ID]
      ,CDPAYAPP.[CLSVC_ID]
      ,ISNULL(CDCLSVC.[EXTENDED_PRICE],0) AS EXTENTED_PRICE
      ,ISNULL(CDCLSVC.[BALANCE],0) AS BALANCE
      ,ISNULL(MAX(CDPAYAPP.[ALLOWED_AMT]),0) AS AllowedAmt
      ,ISNULL(MAX(CDPAYAPP.[ADJ_AMT]),0) AS AdjAmt
      ,ISNULL(MAX(CDPAYAPP.[WRITE_OFF_AMT]),0)AS WriteOff
      ,ISNULL(SUM(CDPAYAPP.[PAY_APPLIED_AMT]),0)AS AppliedAmt
      ,ISNULL(TBL1.SVC_BALANCE,0) AS SVC_BALANCE
       ,CDCLSVC.[EMP_ID]
       ,CDCLSVC.[PLACE_ID]
       ,CDCLSVC.[BEG_DATE]
       ,CDCLSVC.[UNIT_ID]
       ,CDCLSVC.[SUB_UNIT_ID]
  FROM [AnaDEV].[dbo].[CDCLSVC]

  LEFT OUTER JOIN CDPAYAPP
  ON CDCLSVC.ID = CDPAYAPP.CLSVC_ID

 LEFT OUTER JOIN
            (SELECT SVC_BALANCE
            ,CLSVC_ID
            FROM CDPAYAPP
            WHERE CDPAYAPP.RECNUM IN 
                (SELECT MAX([RECNUM])
                  FROM [AnaDEV].[dbo].[CDPAYAPP]
                  GROUP BY CLSVC_ID)) AS TBL1

            ON TBL1.CLSVC_ID = CDPAYAPP.CLSVC_ID

    WHERE [CDCLSVC].[BEG_DATE] between '2015-07-01' and DATEADD(DAY,-(DATEPART(DAY, getdate())),GETDATE())
    GROUP BY CDCLSVC.ID,CDCLSVC.[CLIENT_ID],CDPAYAPP.CLSVC_ID,CDCLSVC.
            [EXTENDED_PRICE],CDCLSVC.[BALANCE],CDCLSVC.[EMP_ID] 
        ,CDCLSVC.[BEG_DATE],TBL1.SVC_BALANCE, CDCLSVC.PLACE_ID
         ,CDCLSVC.[UNIT_ID] ,CDCLSVC.[SUB_UNIT_ID]  
    ORDER BY CDCLSVC.BEG_DATE DESC   -- CHECKING THE WHERE CLAUSE