首先,我知道这是一个糟糕设计的情况,但我只是想知道如何从所需信息的联合中获得单个sql结果。
所以这是一个简单的例子,2个表与id连接:
TableA TableB
id code id tableA_id (FK) start end
---- ------- ---- ---------------- ------- ------
1 codeA 1 2 NY null
2 codeB 2 2 null LA
因此,TableA
中的第2行在TableB
中有2条相应的行。
期望的结果:
tableA_code tableB_start tableB_end
------------- -------------- ------------
codeB NY LA
因此,在结果中需要将表B中的2行分组为一行并消除空值。
答案 0 :(得分:3)
查询:
select A.code, B1.start, B2.end
from TableB B1, TableB B2, TableA A
where B1.tableA_id = B2.tableA_id
and B1.tableA_id = A.id
and B1.start is not null and B2.end is not null
答案 1 :(得分:0)
Let me know if this works for you
SELECT A.code,
MAX(B.STRT) STRT,
MAX(B.END)
END
FROM
(SELECT 1 AS A_ID,'CODE A' AS CODE FROM DUAL
UNION ALL
SELECT 2 AS A_ID,'CODE B' AS CODE FROM DUAL
)A,
(SELECT 1 AS B_ID,2 AS A_ID,'NY' STRT, NULL AS END FROM DUAL
UNION ALL
SELECT 2 AS B_ID,2 AS A_ID, NULL STRT, 'LA' AS END FROM DUAL
)B
WHERE A.A_ID = B.A_ID
GROUP BY A.code;