连接到多个事务表时,SQL查询乘以SUM结果

时间:2017-07-26 10:24:29

标签: sql sql-server tsql

我正在编制一份报告,根据分配和取消日期衡量现金表现。为了衡量这一点,我查询了一个交易子查询,该查询撤回了所有交易并加入了账号和交易日期。然后将其汇总在查询的顶部。

使用一个子查询时一切正常但是我现在不得不添加额外的事务子查询; 1从分配日期(到取消日期或今天,如果值为空)计算实际付款,并计算从分配日期+ 14天(到取消日期或今天,如果值为空)的任何已取消付款。

第一个子查询完美无缺,但现在我已经添加了第二个查询,它将求和值乘以8.过去尝试查询此表两次时,我已经有了这个。 (并不总是8)。有什么想法吗?

谢谢

总计这些的总和(TEST1工作正常,TEST2没那么多)和子查询:

SUM(CASE WHEN v1.[Transaction Date1] >= dateadd(DD, 1, EOMONTH(getdate(), -1)) 
            THEN v1.[Payments] ELSE 0 
    END) AS TEST1,
SUM(CASE WHEN v2.[Transaction Date2] >= dateadd(DD, 1, EOMONTH(getdate(), -1)) 
            THEN v2.[Payments2] ELSE 0 
    END) AS TEST2,

我的代码:

LEFT JOIN 
    (SELECT 
         [Account Number1], [Transaction Date1], SUM([Payments1]) AS [Payments]
     FROM 
         (SELECT
              ACCOUNTNO AS [Account Number1],
              TRANDT AS [Transaction Date1],
              CASE 
                 WHEN TRANSACTIONTYPE = 'Credit' 
                    THEN TRANAMT * -1
                    ELSE TRANAMT
              END [Payments1]
          FROM 
              TRANSACTIONS t WITH (NOLOCK)
          JOIN 
              TRANSACTIONDETAILS td WITH (NOLOCK) ON td.TRANSACTIONID = t.TRANSACTIONS1
          JOIN 
              TRANSACTIONS a WITH (NOLOCK) ON a.ID = TRANSACTIONS1
                                           AND [TRANTYPE] LIKE '%PAY%'
                                           AND [TRANAMT] <> 0
          INNER JOIN 
              ACCOUNTS na WITH (NOLOCK) ON a.ACCOUNTID = ACCOUNTS1
          WHERE 
              TRANSACTIONTYPE = 'Credit') v
     GROUP BY 
         [Account Number1], [Transaction Date1]) v1 ON MAINT.[ACCNO] = v1.[Account Number1]
                                                    AND MAINT.[Allocation Date] <= v1.[Transaction Date1]
                                                    AND v1.[Transaction Date1] <=  MAINT.[Transaction to date]
LEFT JOIN 
    (SELECT 
         [Account Number2], [Transaction Date2], SUM([Payments2]) AS [Payments2]
     FROM 
         (SELECT
              ACCOUNTNO AS [Account Number2],
              TRANDT AS [Transaction Date2],
              CASE  
                 WHEN TRANSACTIONTYPE = 'Credit' 
                    THEN TRANAMT * -1
                    ELSE TRANAMT
              END [Payments2]
          FROM 
              TRANSACTIONS t WITH (NOLOCK)
          INNER JOIN 
              TRANSACTIONDETAILS td WITH (NOLOCK) ON td.TRANSACTIONID = t.TRANSACTIONS1
          INNER JOIN 
              TRANSACTIONS a WITH (NOLOCK) ON a.ID = TRANSACTIONS1
                                           AND [TRANTYPE] LIKE '%PAY%'
                                           AND [TRANAMT] <> 0
          INNER JOIN 
              ACCOUNTS na WITH (NOLOCK) ON a.ACCOUNTID = ACCOUNTS1
          WHERE 
              TRANSACTIONTYPE = 'Debit') v
      GROUP BY 
          [Account Number2], [Transaction Date2]) v2 ON MAINT.[ACCNO] = v2.[Account Number2]
                                                     AND DATEADD(DD,10,MAINT.[DCA Allocation Date]) <= v2.[Transaction Date2]
                                                     AND v2.[Transaction Date2] <=  MAINT.[Transaction to date]

1 个答案:

答案 0 :(得分:0)

由于两个连接中最内层的子选择是相同的,所以你应该将它放在顶部的WITH语句中,然后只使用它两次。 像这样:

WITH v_CTE
AS  
(SELECT
              ACCOUNTNO AS [Account Number2],
              TRANDT AS [Transaction Date2],
              CASE  
                 WHEN TRANSACTIONTYPE = 'Credit' 
                    THEN TRANAMT * -1
                    ELSE TRANAMT
              END [Payments2]
          FROM 
              TRANSACTIONS t WITH (NOLOCK)
          INNER JOIN 
              TRANSACTIONDETAILS td WITH (NOLOCK) ON td.TRANSACTIONID = t.TRANSACTIONS1
          INNER JOIN 
              TRANSACTIONS a WITH (NOLOCK) ON a.ID = TRANSACTIONS1
                                           AND [TRANTYPE] LIKE '%PAY%'
                                           AND [TRANAMT] <> 0
          INNER JOIN 
              ACCOUNTS na WITH (NOLOCK) ON a.ACCOUNTID = ACCOUNTS1
          WHERE 
              TRANSACTIONTYPE = 'Debit') 
...
LEFT JOIN 
    (SELECT 
         [Account Number1], [Transaction Date1], SUM([Payments1]) AS [Payments]
     FROM 
         v_CTE v
     GROUP BY 
         [Account Number1], [Transaction Date1]) v1 ON MAINT.[ACCNO] = v1.[Account Number1]
                                                    AND MAINT.[Allocation Date] <= v1.[Transaction Date1]
                                                    AND v1.[Transaction Date1] <=  MAINT.[Transaction to date]
LEFT JOIN 
    (SELECT 
         [Account Number2], [Transaction Date2], SUM([Payments2]) AS [Payments2]
     FROM 
         v_CTE v
      GROUP BY 
          [Account Number2], [Transaction Date2]) v2 ON MAINT.[ACCNO] = v2.[Account Number2]
                                                     AND DATEADD(DD,10,MAINT.[DCA Allocation Date]) <= v2.[Transaction Date2]
                                                     AND v2.[Transaction Date2] <=  MAINT.[Transaction to date]