此问题是此post的扩展 我想用SQLite加入四个不同的表,它们只有两个共同的列。但是,假设有30多列,即不仅仅列 a - h 。请看下面的例子
表1:
a b lon lat
---------------
22 33 11 22
表2:
c d lon lat
---------------
1 2 44 45
表3
e f lon lat
-----------------------
NULL NULL 100 101
表4
g h lon lat
-----------------------
NULL NULL 200 201
目前的解决方案如下
SELECT a,b,NULL AS c, NULL AS d,NULL AS e, NULL AS f, NULL AS g, NULL AS h,
lon,lat
FROM table1
UNION ALL
SELECT NULL, NULL,c,d,NULL AS e, NULL AS f, NULL AS g, NULL AS h, lon,lat
FROM table2
UNION ALL
SELECT NULL, NULL,NULL,NULL,e,f, NULL AS g, NULL AS h, lon,lat
FROM table3
UNION ALL
SELECT NULL, NULL,NULL,NULL,NULL,NULL,g,h, lon,lat
from table4
结果:
+------+------+------+------+------+------+------+------+-----+-----+
| a | b | c | d | e | f | g | h | lon | lat |
+------+------+------+------+------+------+------+------+-----+-----+
| 22 | 33 | NULL | NULL | NULL | NULL | NULL | NULL | 11 | 22 |
| NULL | NULL | 1 | 2 | NULL | NULL | NULL | NULL | 44 | 45 |
| NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | 100 | 101 |
| NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | 200 | 201 |
+------+------+------+------+------+------+------+------+-----+-----+
答案 0 :(得分:1)
当你有30多列时,我不相信这是一种特别巧妙的方法。以下是我能做的最好的事情,使用嵌套的CTE来实现full outer joins,然后使用coalesce
来选择第一个非空的lat / lon。
仍需要在最高SELECT
语句中枚举所有30多个字段,但至少需要NULL AS ...
的大量列表:
SELECT
a, b, c, d, e, f, g, h,
coalesce(lat1, lat2, lat3, lat4) AS lat,
coalesce(lon1, lon2, lon3, lon4) AS lon
FROM (
WITH t1_x_t2 AS (
SELECT t1.*, t2.*,
t1.lat AS lat1, t2.lat AS lat2, t1.lon AS lon1, t2.lon AS lon2
FROM table1 t1 LEFT OUTER JOIN table2 t2 ON 0
UNION ALL
SELECT t1.*, t2.*,
t1.lat AS lat1, t2.lat AS lat2, t1.lon AS lon1, t2.lon AS lon2
FROM table2 t2 LEFT OUTER JOIN table1 t1 ON 0
), t3_x_t4 AS (
SELECT t3.*, t4.*,
t3.lat AS lat3, t4.lat AS lat4, t3.lon AS lon3, t4.lon AS lon4
FROM table3 t3 LEFT OUTER JOIN table4 t4 ON 0
UNION ALL
SELECT t3.*, t4.*,
t3.lat AS lat3, t4.lat AS lat4, t3.lon AS lon3, t4.lon AS lon4
FROM table4 t4 LEFT OUTER JOIN table3 t3 ON 0
)
SELECT t1_x_t2.*, t3_x_t4.* FROM t1_x_t2 LEFT OUTER JOIN t3_x_t4 ON 0
UNION ALL
SELECT t1_x_t2.*, t3_x_t4.* FROM t3_x_t4 LEFT OUTER JOIN t1_x_t2 ON 0
)
答案 1 :(得分:-1)
使用pivot()
(如果您的dbms
支持)导出结果的一种方法可能如下所示。
select a,b,c,d,e,f,g,h,lon,lat
from (
select 'a' as columnName1, a as val1,'b' as ColumnName2, b as val2, lon,lat
from table1
union all
select 'c',c,'d', d, lon,lat
from table2
union all
select 'e',e,'f', f, lon,lat
from table3
union all
select 'g',g,'h', h, lon,lat
from table4
) t1
PIVOT (MAX(val1)
FOR columnName1 IN (a,c,e,g)) as Pivot1
PIVOT (MAX(val2)
FOR columnName2 IN (b,d,f,h)) AS Pivot2;
<强>结果:强>
+------+------+------+------+------+------+------+------+-----+-----+
| a | b | c | d | e | f | g | h | lon | lat |
+------+------+------+------+------+------+------+------+-----+-----+
| 22 | 33 | NULL | NULL | NULL | NULL | NULL | NULL | 11 | 22 |
| NULL | NULL | 1 | 2 | NULL | NULL | NULL | NULL | 44 | 45 |
| NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | 100 | 101 |
| NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | 200 | 201 |
+------+------+------+------+------+------+------+------+-----+-----+
<强> DEMO 强>
P.S。请注意,上述查询适用于sql-Server
,因此您可能需要为dbms
进行调整。