我在下面的代码中定义了一个名为a.onhold_endtime的字段。我希望将该值作为整个SELECT语句的一部分返回,但是当我包含它时,别名a。*无法识别。它返回一个错误" ERROR:缺少FROM-clause条目表#34; a" "
Select distinct
woas.workorderid,
a.onhold_endtime,
(SELECT
(SELECT count(*) AS work_hours
FROM generate_series (b.onhold_starttime
, a.onhold_endtime - interval '1h'
, interval '1h') h
WHERE EXTRACT(ISODOW FROM h) < 6
AND h::time >= '08:00'
AND h::time <= '18:00')
FROM (
SELECT DISTINCT woas.workorderid,
timestamp 'epoch' +
nth_value(wos.endtime,1) OVER(PARTITION BY woas.workorderid ORDER BY wos.endtime ASC
ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING)
/1000 * INTERVAL '1 second' as onhold_endtime from wo_status_info wos
LEFT JOIN wo_assessment woas ON woas.assessmentid = wos.assessmentid
WHERE tmp.workorderid = woas.workorderid AND wos.statusid = 1 AND wos.nextstatusid = 2
) as a
LEFT JOIN (
SELECT DISTINCT woas.workorderid,
timestamp 'epoch' +
nth_value(wos.endtime,1) OVER(PARTITION BY woas.workorderid ORDER BY wos.endtime ASC
ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING)
/1000 * INTERVAL '1 second' as onhold_starttime from wo_status_info wos
LEFT JOIN wo_assessment woas ON woas.assessmentid = wos.assessmentid
WHERE
(wos.statusid = 2 AND wos.nextstatusid <> 2)
) as b ON a.workorderid = b.workorderid) AS onhold_difference1
FROM wo_assessment as tmp
LEFT JOIN wo_assessment woas ON tmp.assessmentid = woas.assessmentid
LEFT JOIN wo_status_info wos ON woas.assessmentid = wos.assessmentid
ORDER BY woas.workorderid ASC
有没有办法可以构建代码,这样我就可以使用&#34; a&#34;别名?
使用简化代码进行更新 我定义了一个名为&#34; onhold_endtime&#34;在子查询中,别名为&#34; a&#34;。我想在我的主SELECT语句中返回值a.onhold_endtime
Select distinct
woas.workorderid,
a.onhold_endtime,
(SELECT
(SELECT count(*))
FROM (
SELECT DISTINCT woas.workorderid,
nth_value(wos.endtime,1) OVER(PARTITION BY woas.workorderid ORDER BY wos.endtime ASC
ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING)
as onhold_endtime from wo_status_info wos
WHERE y.workorderid = woas.workorderid AND wos.statusid = 1 AND wos.nextstatusid = 2
) as a
LEFT JOIN (
SELECT DISTINCT woas.workorderid,
nth_value(wos.endtime,1) OVER(PARTITION BY woas.workorderid ORDER BY wos.endtime ASC
ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING)
as onhold_starttime from wo_status_info wos
WHERE
(wos.statusid = 2 AND wos.nextstatusid <> 2)
) as b ON a.workorderid = b.workorderid) AS x
FROM wo_assessment as y
LEFT JOIN wo_assessment woas ON y.assessmentid = woas.assessmentid
答案 0 :(得分:0)
简而言之 - 您可以在相关子查询中使用外部查询别名:
t=# select o.oid, (select o.datname||'*') from pg_database o limit 1;
oid | ?column?
-------+-----------
13505 | postgres*
(1 row)
但不能做相反的事情:
t=# select b.*, (select o.datname||'*') b from pg_database o limit 1;
ERROR: missing FROM-clause entry for table "b"
LINE 1: select b.*, (select o.datname||'*') b from pg_database o lim...
^
最接近预期的是:
t=# select oid,b.* from pg_database o, LATERAL (select o.datname||'*') b limit 1;
oid | ?column?
-------+-----------
13505 | postgres*
(1 row)