我在PostgreSQL中有这样的视图。:
SELECT met1.tiempo,
met1.pyrseg AS met1_pyrseg,
met1.pyrhor AS met1_pyrhor,
met2.pyrseg AS met2_pyrseg,
met2.pyrhor AS met2_pyrhor,
met3.pyrseg AS met3_pyrseg,
met3.pyrhor AS met3_pyrhor
FROM
(SELECT meteo_hour.tstamp AS tiempo,
avg(meteo_hour.avg_globalradiation1) AS pyrseg,
avg(meteo_hour.avg_globalradiation2) AS pyrhor
FROM dw.meteo_hour
WHERE meteo_hour.meteo = 0
GROUP BY meteo_hour.tstamp) met1,
(SELECT meteo_hour.tstamp AS tiempo,
avg(meteo_hour.avg_globalradiation1) AS pyrseg,
avg(meteo_hour.avg_globalradiation2) AS pyrhor
FROM dw.meteo_hour
WHERE meteo_hour.meteo = 25
GROUP BY meteo_hour.tstamp) met2,
(SELECT meteo_hour.tstamp AS tiempo,
avg(meteo_hour.avg_globalradiation1) AS pyrseg,
avg(meteo_hour.avg_globalradiation2) AS pyrhor
FROM dw.meteo_hour
WHERE meteo_hour.meteo = 35
GROUP BY meteo_hour.tstamp) met3
WHERE met1.tiempo = met2.tiempo AND met3.tiempo = met1.tiempo
当3个子查询中的一个失败时,会出现问题。由于有一个子查询没有值,因此最后一个WHERE失败,因为其中一个比较是针对空值。
如果有空值或者为空,我如何丢弃子查询?
答案 0 :(得分:1)
您应该使用FULL JOIN
。通过其他一些改进,您将获得:
SELECT tiempo, met1_pyrseg, met1_pyrhor,
met2_pyrseg, met2_pyrhor,
met3_pyrseg, met3_pyrhor
FROM (
SELECT tstamp AS tiempo,
avg(avg_globalradiation1) AS met1_pyrseg,
avg(avg_globalradiation2) AS met1_pyrhor
FROM dw.meteo_hour
WHERE meteo = 0
GROUP BY tstamp) met1
FULL JOIN (
SELECT tstamp AS tiempo,
avg(avg_globalradiation1) AS met2_pyrseg,
avg(avg_globalradiation2) AS met2_pyrhor
FROM dw.meteo_hour
WHERE meteo = 25
GROUP BY tstamp) met2 USING (tiempo)
FULL JOIN (
SELECT tstamp AS tiempo,
avg(avg_globalradiation1) AS met3_pyrseg,
avg(avg_globalradiation2) AS met3_pyrhor
FROM dw.meteo_hour
WHERE meteo = 35
GROUP BY tstamp) met3 USING (tiempo)
ORDER BY tiempo;