Combining 2 SQL queries I have these 2 queries below that I use to calculate net profits from Jan to Dec.

Query 1

/* Profit for the Period 13 */
SELECT * FROM
(
SELECT MONTH(T0.RefDate) AS Month, '311999' AS 'SAP Code', 'Period Profit' AS 'Description', SUM (T0.[Credit] - T0.[Debit]) AS 'Amount',
'311200' AS 'FatherNum', 12 AS 'Group', 'Liabilities' [Liabilities] 
FROM JDT1 T0 WHERE (T0.RefDate BETWEEN DATEADD(yy, DATEDIFF(yy,0,'20151231'), 0) AND '20151231') 
AND T0.[ContraAct] &lt;&gt; '311201'
GROUP BY T0.RefDate) AS q
PIVOT
(
SUM(Amount) 
FOR [Month] IN ([1],[2],[3],[4],[5],[6],[7],[8],[9],[10],[11],[12])
) AS query


Results:



Query 2

/* Profit for the Period 13 */
SELECT * FROM
(
SELECT MONTH(T1.RefDate) AS Month, '311999' AS 'SAP Code', 'Period Profit' AS 'Description', SUM (T1.[Credit] - T1.[Debit]) AS 'Amount',
'311200' AS 'FatherNum', 12 AS 'Group', 'Liabilities' [Liabilities] 
FROM OACT T0 INNER JOIN JDT1 T1 ON T0.[AcctCode] = T1.[Account]
WHERE (T1.RefDate BETWEEN DATEADD(yy, DATEDIFF(yy,0,'20151231'), 0) AND '20151231') 
AND T0.GroupMask &gt; 3 
GROUP BY T1.RefDate) AS q 
PIVOT
(
SUM(Amount) 
FOR [Month] IN ([1],[2],[3],[4],[5],[6],[7],[8],[9],[10],[11],[12])
) AS query


Results:



The difference between the 2 comes in month 12. Query 1 accurately gets the Dec profit because it is an account that stores data after a process of closing the 2015 year in December.

How do I combine the two into one query such that I get all results of query 2 except for month 12 instead of -4764758.550000 I get 3280335.630000?
I have created 2 table variables in an attempt to match your environment.
I have added an UNION ALL to your original query, one query selects all other months but december, and the second selects only december.

there are other ways to do this, but in any way, it looks like an expensive query.

here we go:

SET NOCOUNT ON
SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED

declare @JDT1 table(

ContraAct char(6) primary key clustered,
Account int,
RefDate datetime,
credit money,
debit money

)


declare @OACT table(
AcctCode int primary key clustered,
GroupMask int
)

insert into  @JDT1 (ContraAct,Account,RefDate,credit,debit)
values ('311202',1,'20151231', 10,7)

insert into  @JDT1 (ContraAct,Account,RefDate,credit,debit)
values ('311203',1,'20151231', 10,7)


insert into  @JDT1 (ContraAct,Account,RefDate,credit,debit)
values ('311204',2,'20151231', 10,7)

insert into  @JDT1 (ContraAct,Account,RefDate,credit,debit)
values ('311205',2,'20151231',10,7)


insert into  @JDT1 (ContraAct,Account,RefDate,credit,debit)
values ('311206',1,'20151031', 10,7)

insert into  @JDT1 (ContraAct,Account,RefDate,credit,debit)
values ('311207',1,'20151031', 10,7)


insert into  @JDT1 (ContraAct,Account,RefDate,credit,debit)
values ('311208',2,'20151031', 10,7)

insert into  @JDT1 (ContraAct,Account,RefDate,credit,debit)
values ('311209',2,'20151031',10,7)

insert into @OACT(AcctCode,GroupMask)
values (2,4)

/* Profit for the Period 13 */
SELECT * FROM
(
        SELECT MONTH(T0.RefDate) AS Month, 
              '311999' AS 'SAP Code', 
              'Period Profit' AS 'Description', 
              SUM (T0.[Credit] - T0.[Debit]) AS 'Amount',
              '311200' AS 'FatherNum', 
              12 AS 'Group', 
              'Liabilities' [Liabilities] 
        FROM @JDT1 T0 
       WHERE (T0.RefDate 
              BETWEEN DATEADD(yy, DATEDIFF(yy,0,'20151231'), 0) 
              AND '20151231') 
        AND T0.[ContraAct] &lt;&gt; '311201'
        AND MONTH(T0.RefDate) = 12
        GROUP BY T0.RefDate

        UNION ALL


        SELECT MONTH(T1.RefDate) AS Month, 
              '311999' AS 'SAP Code', 
              'Period Profit' AS 'Description', 
              SUM (T1.[Credit] - T1.[Debit]) AS 'Amount',
              '311200' AS 'FatherNum', 
              12 AS 'Group', 
              'Liabilities' [Liabilities] 
        FROM @OACT T0 
             INNER JOIN @JDT1 T1 
                     ON T0.[AcctCode] = T1.[Account]
        WHERE (T1.RefDate 
               BETWEEN DATEADD(yy, DATEDIFF(yy,0,'20151231'), 0) 
                   AND '20151231') 
        AND T0.GroupMask &gt; 3 
        AND MONTH(T1.RefDate) &lt;&gt; 12
        GROUP BY T1.RefDate

) AS q
PIVOT
(
SUM(Amount) 
FOR [Month] IN ([1],[2],[3],[4],[5],[6],[7],[8],[9],[10],[11],[12])
) AS query


the results, you can compare october and december.