1
如何從每個結果中添加總和,以便最終結果能夠爲我提供如下表格?SQL幫助,添加結果來創建單個表?
Date | Sum1 | Sum2 1108 | Total sum1 | Total sum2 etc.
DECLARE @StartDate int
DECLARE @EndDate int
SET @StartDate = XXXXXX
SET @EndDate = XXXXXX
SELECT ODATE,
SUM(CASE WHEN [COLUMN1] IN (X,X) THEN 1 ELSE 0 END)+ SUM(CASE WHEN [COLUMN3] IN (X) THEN 1 ELSE 0 END) AS SUM1,
SUM(CASE WHEN [COLUMN2] IN (X,X) THEN 1 ELSE 0 END) AS SUM2
FROM [Db1]..
WHERE ODATE Between @StartDate And @EndDate
UNION ALL
SELECT ODATE,
SUM(CASE WHEN [COLUMN1] IN (X,X) THEN 1 ELSE 0 END)+ SUM(CASE WHEN [COLUMN3] IN (X) THEN 1 ELSE 0 END) AS SUM1,
SUM(CASE WHEN [COLUMN2] IN (X,X) THEN 1 ELSE 0 END) AS SUM2
FROM [Db2]..
WHERE ODATE Between @StartDate And @EndDate
UNION ALL
SELECT ODATE,
SUM(CASE WHEN [COLUMN1] IN (X,X) THEN 1 ELSE 0 END)+ SUM(CASE WHEN [COLUMN3] IN (X) THEN 1 ELSE 0 END) AS SUM1,
SUM(CASE WHEN [COLUMN2] IN (X,X) THEN 1 ELSE 0 END) AS SUM2
FROM [Db3]..
WHERE ODATE Between @StartDate And @EndDate