我有这样的代码:
SELECT ID, SUMof2016, SUMof2015
FROM (SELECT ID, SUMof2016
from table2016) t2016
FULL JOIN
(SELECT ID, SUMof2015
from table2015) t2015 on t2016.ID = t2015.ID
GROUP BY ID,SUMof2016, SUMof2015;
但出现此错误:
ORA-00918“列含糊不清” 所以我改成喜欢
SELECT t2016.ID, SUMof2016, SUMof2015
FROM (SELECT ID, SUMof2016
from table2016) t2016
FULL JOIN
(SELECT ID, SUMof2015
from table2015) t2015 on t2016.ID = t2015.ID
GROUP BY t2016.ID,SUMof2016, SUMof2015;
但我认为这不显示2015年的ID,我想显示2015和2016 ID。
如果出现此错误,如何显示2015年和2016年的ID?
答案 0 :(得分:2)
您可以使用coalesce
获取第一个非空id
:
coalesce(t2015.ID, t2016.ID) as ID
答案 1 :(得分:1)
您的查询不需要GROUP BY
。此外,不需要子查询:
SELECT COALESCE(t2016.ID, t2015.ID) as ID, SUMof2016, SUMof2015
FROM table2016 t2016 FULL JOIN
table2015 t2015
ON t2016.ID = t2015.ID;
如果您希望在两年内没有值的ID中看到0
而不是NULL
:
SELECT COALESCE(t2016.ID, t2015.ID) as ID,
COALESCE(SUMof2016, 0) as SUMof2016,
COALESCE(SUMof2015, 0) as SUMof2015
FROM table2016 t2016 FULL JOIN
table2015 t2015
ON t2016.ID = t2015.ID;