合并两个查询以添加其他列

时间:2016-01-15 08:57:41

标签: sql sql-server sql-server-2012

我想将两个SQL查询合并为一个。第二个应该向第一个添加一个额外的列。

查询#1:

SELECT 
    T3.AcctCode, T3.AcctName,
    SUM(CASE WHEN T0.TaxDate BETWEEN '2015-12-01' AND '2015-12-31'
             THEN T1.Debit - T1.Credit 
             ELSE 0 END)* -1 AS [Balance],
    SUM(T1.Debit - T1.Credit)* -1  AS [YTD Balance],
    (CASE WHEN T3.AcctCode BETWEEN '611110' AND '612000'
       THEN 1 WHEN T3.AcctCode BETWEEN '541100' AND '541950'
       THEN 2 WHEN T3.AcctCode BETWEEN '542100' AND '542700'
       THEN 3 WHEN T3.AcctCode = '543100'
       THEN 4 WHEN T3.AcctCode BETWEEN '511100' AND '511950'
       THEN 5 WHEN T3.AcctCode BETWEEN '512100' AND '512995'
       THEN 6 WHEN T3.AcctCode = '513100'
       THEN 7 ELSE 0 END) AS [Group]
FROM 
    OJDT T0
INNER JOIN 
    JDT1 T1 ON T0.[TransId] = T1.[TransId]
INNER JOIN 
    OACT T2 ON T1.[Account] = T2.[AcctCode]
INNER JOIN 
    OACT T3 ON T3.[AcctCode] = T2.[FatherNum]
WHERE 
    T0.TaxDate BETWEEN '2015-04-01' AND '2015-12-31'     
    AND T2.GroupMask IN(5,6)
GROUP BY 
    T3.AcctCode, T3.AcctName

示例输出

enter image description here

查询#2:

SELECT 
    T1.FatherNum, SUM(T0.CredLTotal) AS [Budget]
FROM 
    OBGT T0
LEFT JOIN 
    OACT T1 ON T0.[AcctCode] = T1.[AcctCode]
WHERE 
    T1.GroupMask IN (5, 6) 
    AND (T1.FatherNum BETWEEN '511100' AND '612000') 
    AND YEAR(T0.FinancYear) = 2015 
GROUP BY 
    T1.FatherNum

示例输出

enter image description here

我想出了这个:

SELECT 
    T3.AcctCode, T3.AcctName,
    SUM(CASE WHEN T0.TaxDate BETWEEN '2015-12-01' AND '2015-12-31'
             THEN T1.Debit - T1.Credit 
             ELSE 0 END)* -1 AS [Balance],
    SUM(T1.Debit - T1.Credit)* -1  AS [YTD Balance],
    SUM(T4.CredLTotal) AS [Budget],
    (CASE WHEN T3.AcctCode BETWEEN '611110' AND '612000'
       THEN 1 WHEN T3.AcctCode BETWEEN '541100' AND '541950'
       THEN 2 WHEN T3.AcctCode BETWEEN '542100' AND '542700'
       THEN 3 WHEN T3.AcctCode = '543100'
       THEN 4 WHEN T3.AcctCode BETWEEN '511100' AND '511950'
       THEN 5 WHEN T3.AcctCode BETWEEN '512100' AND '512995'
       THEN 6 WHEN T3.AcctCode = '513100'
       THEN 7 ELSE 0 END) AS [Group]
FROM 
    OJDT T0
INNER JOIN 
    JDT1 T1 ON T0.[TransId] = T1.[TransId]
INNER JOIN 
    OACT T2 ON T1.[Account] = T2.[AcctCode]
INNER JOIN 
    OACT T3 ON T3.[AcctCode] = T2.[FatherNum]
LEFT JOIN 
    OBGT T4 ON (T4.[AcctCode] = T2.[AcctCode] 
            AND YEAR(T4.FinancYear) = 2015)
WHERE 
    T0.TaxDate BETWEEN '2015-04-01' AND '2015-12-31'     
    AND T2.GroupMask IN (5, 6)     
GROUP BY 
    T3.AcctCode, T3.AcctName, T2.FatherNum
ORDER BY 
    T3.AcctCode

示例输出

enter image description here

但是,列Balance显示的数字不正确,即未返回第二个查询的结果。

如何让SUM(T4.CredLTotal) AS [Budget]正确汇总?

1 个答案:

答案 0 :(得分:1)

如果不查看这些复杂查询的详细信息,您似乎只想简单地将两个查询连接在一起。

在第一个查询中,您按AcctCode, AcctName分组。我假设每个AcctCode结果会有一行,即AcctCode不会在结果集中重复。

在第二个查询中,您按FatherNum分组,因此它也不会在结果集中重复。

您需要的就是将它们结合在一起。我重复了您的查询Common Table Expressions(CTE)并加入了它们。

WITH
CTE_Query1
AS
(
    SELECT 
        T3.AcctCode, T3.AcctName,
        SUM(CASE WHEN T0.TaxDate BETWEEN '2015-12-01' AND '2015-12-31'
                 THEN T1.Debit - T1.Credit 
                 ELSE 0 END)* -1 AS [Balance],
        SUM(T1.Debit - T1.Credit)* -1  AS [YTD Balance],
        (CASE WHEN T3.AcctCode BETWEEN '611110' AND '612000'
           THEN 1 WHEN T3.AcctCode BETWEEN '541100' AND '541950'
           THEN 2 WHEN T3.AcctCode BETWEEN '542100' AND '542700'
           THEN 3 WHEN T3.AcctCode = '543100'
           THEN 4 WHEN T3.AcctCode BETWEEN '511100' AND '511950'
           THEN 5 WHEN T3.AcctCode BETWEEN '512100' AND '512995'
           THEN 6 WHEN T3.AcctCode = '513100'
           THEN 7 ELSE 0 END) AS [Group]
    FROM 
        OJDT T0
    INNER JOIN 
        JDT1 T1 ON T0.[TransId] = T1.[TransId]
    INNER JOIN 
        OACT T2 ON T1.[Account] = T2.[AcctCode]
    INNER JOIN 
        OACT T3 ON T3.[AcctCode] = T2.[FatherNum]
    WHERE 
        T0.TaxDate BETWEEN '2015-04-01' AND '2015-12-31'     
        AND T2.GroupMask IN(5,6)
    GROUP BY 
        T3.AcctCode, T3.AcctName
)
,CTE_Query2
AS
(
    SELECT 
        T1.FatherNum, SUM(T0.CredLTotal) AS [Budget]
    FROM 
        OBGT T0
    LEFT JOIN 
        OACT T1 ON T0.[AcctCode] = T1.[AcctCode]
    WHERE 
        T1.GroupMask IN (5, 6) 
        AND (T1.FatherNum BETWEEN '511100' AND '612000') 
        AND YEAR(T0.FinancYear) = 2015 
    GROUP BY 
        T1.FatherNum
)
SELECT *
FROM
    CTE_Query1
    INNER JOIN CTE_Query2 ON CTE_Query2.FatherNum = CTE_Query1.AcctCode
;