I want to combine the below queries into a single output, I am not sure on how to use union as I have bunch of conditions on the queries as well. In the final output I also need to Subtract the result of negative_sales column from query 2 from Net value column from query 1
SELECT storenumber,
bbbtendertypetext,
SUM(financialamounttendered) - SUM(financialchangeamount) AS NET_VALUE,
COUNT(transactionid) AS Transaction_Count
FROM [AceTLogData].[Tlog].[tender]
WHERE transactiondatetime > '2022-09-21 00:00:00.000'
AND transactiondatetime < '2022-09-22 00:03:00.000'
GROUP BY storenumber, bbbtendertypetext
ORDER BY storenumber
SELECT storenumber,
bbbtendertypetext,
SUM(financialamounttendered) AS negative_Net_Sales,
COUNT(transactionid) AS Transaction_Count
FROM [AceTLogData].[Tlog].[tender_correct]
where transactiondatetime > '2022-09-21 00:00:00.000'
AND transactiondatetime < '2022-09-22 00:03:00.000'
GROUP BY storenumber, bbbtendertypetext
ORDER BY storenumber