这是我的代码:
drop table if exists #Table_A
drop table if exists #Table_B
drop table if exists #Table_C
create table #Table_A
(
[DATE] Date,
TYPE INT,
QTY INT
);
create table #Table_B
(
[DATE] Date,
TYPE INT,
QTY INT
);
create table #Table_C
(
[DATE] Date,
TYPE INT,
QTY INT
);
Insert into #Table_A VALUES ('2018-01-01', 1, 10)
Insert into #Table_B VALUES ('2018-01-01', 1, 5)
Insert into #Table_B VALUES ('2018-01-01', 2, 10)
Insert into #Table_B VALUES ('2018-01-01', 3, 10)
Insert into #Table_C VALUES ('2018-01-01', 1, 5)
Insert into #Table_C VALUES ('2018-01-01', 2, 10)
Insert into #Table_C VALUES ('2018-01-01', 3, 10)
查询:
select
A.[DATE],
SUM(CASE WHEN A.[TYPE] = 1 THEN A.QTY END) A_QTY,
SUM(CASE WHEN B.[TYPE] = 1 THEN B.QTY END) B_QTY,
SUM(CASE WHEN C.[TYPE] = 1 THEN C.QTY END) C_QTY,
SUM(CASE WHEN A.[TYPE] = 2 THEN A.QTY END) A_QTY,
SUM(CASE WHEN B.[TYPE] = 2 THEN B.QTY END) B_QTY,
SUM(CASE WHEN C.[TYPE] = 2 THEN C.QTY END) C_QTY,
SUM(CASE WHEN A.[TYPE] = 3 THEN A.QTY END) A_QTY,
SUM(CASE WHEN B.[TYPE] = 3 THEN B.QTY END) B_QTY,
SUM(CASE WHEN C.[TYPE] = 3 THEN C.QTY END) C_QTY
FROM
#Table_A A
INNER JOIN #Table_B B ON A.DATE = B.DATE AND A.TYPE = B.TYPE
INNER JOIN #Table_C C ON A.DATE = C.DATE AND A.TYPE = C.TYPE
GROUP BY A.[DATE]
当前结果:
DATE | A_QTY | B_QTY | C_QTY A_QTY B_QTY C_QTY A_QTY B_QTY C_QTY
2018-01-01 | 10 | 5 | 5 NULL NULL NULL NULL NULL NULL
预期结果:
DATE | A_QTY | B_QTY | C_QTY A_QTY B_QTY C_QTY A_QTY B_QTY C_QTY
2018-01-01 | 10 | 5 | 5 NULL 10 10 NULL 10 10
请让我知道如何获得预期的结果。 (我知道当前由于inner join
而无法正常工作。)
答案 0 :(得分:0)
查询的问题在于,由于它使用INNER JOIN
,因此仅计算所有3个表中都存在的记录/日期元组。
如果存在一个表,该表可以始终始终具有所有(TYPE
,DATE
)元组(在示例数据中,则为Table_C
),那么您可以将其用作起点并使用LEFT JOIN
s:
FROM #Table_C C
LEFT JOIN #Table_A A ON A.DATE = C.DATE AND A.TYPE = C.TYPE
LEFT JOIN #Table_B B ON B.DATE = C.DATE AND B.TYPE = C.TYPE
否则,您可能希望将INNER JOIN
更改为FULL OUTER JOIN
s:
FROM #Table_A A
FULL OUTER JOIN #Table_B B ON A.DATE = B.DATE AND A.TYPE = B.TYPE
FULL OUTER JOIN #Table_C C ON A.DATE = C.DATE AND A.TYPE = C.TYPE
FULL OUTER JOIN
解决方案需要修改SELECT
和GROUP BY
子句,因为日期可能来自3个表中的任何一个。
查询:
SELECT
COALESCE(A.[DATE], B.[DATE], C.[DATE]) DATE,
SUM(CASE WHEN A.[TYPE] = 1 THEN A.QTY END) A_QTY,
SUM(CASE WHEN B.[TYPE] = 1 THEN B.QTY END) B_QTY,
SUM(CASE WHEN C.[TYPE] = 1 THEN C.QTY END) C_QTY,
SUM(CASE WHEN A.[TYPE] = 2 THEN A.QTY END) A_QTY,
SUM(CASE WHEN B.[TYPE] = 2 THEN B.QTY END) B_QTY,
SUM(CASE WHEN C.[TYPE] = 2 THEN C.QTY END) C_QTY,
SUM(CASE WHEN A.[TYPE] = 3 THEN A.QTY END) A_QTY,
SUM(CASE WHEN B.[TYPE] = 3 THEN B.QTY END) B_QTY,
SUM(CASE WHEN C.[TYPE] = 3 THEN C.QTY END) C_QTY
FROM
#Table_A A
FULL OUTER JOIN #Table_B B ON A.DATE = B.DATE AND A.TYPE = B.TYPE
FULL OUTER JOIN #Table_C C ON A.DATE = C.DATE AND A.TYPE = C.TYPE
GROUP BY COALESCE(A.[DATE], B.[DATE], C.[DATE])
此 demo on DB Fiddle 返回:
DATE | A_QTY | B_QTY | C_QTY | A_QTY | B_QTY | C_QTY | A_QTY | B_QTY | C_QTY
:------------------ | ----: | ----: | ----: | ----: | ----: | ----: | ----: | ----: | ----:
01/01/2018 00:00:00 | 10 | 5 | 5 | | 10 | 10 | | 10 | 10