使用 SQL 连接合并来自 4 table 秒的数据行,而不重复行 ID,如下所示的所需结果 table 中描述的
Using a SQL join to combine data rows from 4 tables without repeating row id's as depicted in the desired results table shown below
Table一个
Table B
Table C
TableD
想要的结果
在此先感谢您为实现本案例中的预期结果而提供的任何帮助。我的目标是以这种方式将多个表拼接在一起,假设我的数据库不支持使用 os FULL JOIN 语句。
这样就可以了。
Select
coalesce(Col1, '0') as Col1,
coalesce(Col2, '0') as Col2,
coalesce(Col3, '0') as Col3,
coalesce(Col4, '0') as Col4
(
select ID from TableA union
select ID from TableB union
select ID from TableC union
select ID from TableD
) TbLID
left join TableA on TableA.ID = TbLID.ID
left join TableB on TableB.ID = TbLID.ID
left join TableC on TableC.ID = TbLID.ID
left join TableD on TableD.ID = TbLID.ID
您可以使用 union all
和条件聚合来模拟 full join
:
select
id,
coalesce(max(case when idx = 1 then col end), '0') col1,
coalesce(max(case when idx = 2 then col end), '0') col2,
coalesce(max(case when idx = 3 then col end), '0') col3,
coalesce(max(case when idx = 4 then col end), '0') col4
from (
select id, 1 idx, col1 col from table1
union all select id, 2, col2 from table2
union all select id, 3, col3 from table3
union all select id, 4, col4 from table4
) t
group by id
order by id
您可以使用 UNION ALL
来获得 1 个包含所有 4 个组合的结果集。
然后您可以按 ID
.
分组
并使用条件聚合来转换 table 源上的 COL 值。
SELECT ID
, MAX(CASE WHEN Src = 1 THEN Col ELSE '0' END) AS COL1
, MAX(CASE WHEN Src = 2 THEN Col ELSE '0' END) AS COL2
, MAX(CASE WHEN Src = 3 THEN Col ELSE '0' END) AS COL3
, MAX(CASE WHEN Src = 4 THEN Col ELSE '0' END) AS COL4
FROM
(
SELECT ID, 1 AS Src, COL1 AS Col
FROM TableA
UNION ALL
SELECT ID, 2, COL2
FROM TableB
UNION ALL
SELECT ID, 3, COL3
FROM TableC
UNION ALL
SELECT ID, 4, COL4
FROM TableD
) q
GROUP BY ID
ORDER BY ID
UNION ALL
表格,将每一列放在正确的位置。 GROUP BY
结果。
select id, max(col1), max(col2), max(col3), max(col4)
from
(
select id, col1, null as col2, null as col3, null as col4 from tableA
union all
select id, null, col2, null, null from tableB
union all
select id, null, null, col3, null from tableC
union all
select id, null, null, null, col4 from tableD
) dt
group by id
如果您需要在结果中使用“0”而不是空值,请使用 COALESCE()
select id, coalesce(max(col1),'0'), ...
Table一个
Table B
Table C
TableD
想要的结果
在此先感谢您为实现本案例中的预期结果而提供的任何帮助。我的目标是以这种方式将多个表拼接在一起,假设我的数据库不支持使用 os FULL JOIN 语句。
这样就可以了。
Select
coalesce(Col1, '0') as Col1,
coalesce(Col2, '0') as Col2,
coalesce(Col3, '0') as Col3,
coalesce(Col4, '0') as Col4
(
select ID from TableA union
select ID from TableB union
select ID from TableC union
select ID from TableD
) TbLID
left join TableA on TableA.ID = TbLID.ID
left join TableB on TableB.ID = TbLID.ID
left join TableC on TableC.ID = TbLID.ID
left join TableD on TableD.ID = TbLID.ID
您可以使用 union all
和条件聚合来模拟 full join
:
select
id,
coalesce(max(case when idx = 1 then col end), '0') col1,
coalesce(max(case when idx = 2 then col end), '0') col2,
coalesce(max(case when idx = 3 then col end), '0') col3,
coalesce(max(case when idx = 4 then col end), '0') col4
from (
select id, 1 idx, col1 col from table1
union all select id, 2, col2 from table2
union all select id, 3, col3 from table3
union all select id, 4, col4 from table4
) t
group by id
order by id
您可以使用 UNION ALL
来获得 1 个包含所有 4 个组合的结果集。
然后您可以按 ID
.
分组
并使用条件聚合来转换 table 源上的 COL 值。
SELECT ID
, MAX(CASE WHEN Src = 1 THEN Col ELSE '0' END) AS COL1
, MAX(CASE WHEN Src = 2 THEN Col ELSE '0' END) AS COL2
, MAX(CASE WHEN Src = 3 THEN Col ELSE '0' END) AS COL3
, MAX(CASE WHEN Src = 4 THEN Col ELSE '0' END) AS COL4
FROM
(
SELECT ID, 1 AS Src, COL1 AS Col
FROM TableA
UNION ALL
SELECT ID, 2, COL2
FROM TableB
UNION ALL
SELECT ID, 3, COL3
FROM TableC
UNION ALL
SELECT ID, 4, COL4
FROM TableD
) q
GROUP BY ID
ORDER BY ID
UNION ALL
表格,将每一列放在正确的位置。 GROUP BY
结果。
select id, max(col1), max(col2), max(col3), max(col4)
from
(
select id, col1, null as col2, null as col3, null as col4 from tableA
union all
select id, null, col2, null, null from tableB
union all
select id, null, null, col3, null from tableC
union all
select id, null, null, null, col4 from tableD
) dt
group by id
如果您需要在结果中使用“0”而不是空值,请使用 COALESCE()
select id, coalesce(max(col1),'0'), ...