Postgresql SQL Select 表 1 中的项目基于表 2 中的条件
Postgresql SQL Select items from table1 based on a condition from table2
我正在尝试 select table1 中的项目,其中有一个子 table2 涉及第三个 table3。
Select j.ccmasterid,
(Select sum(i.ccmatpullqty) From table2 i
Where i.ccmasterid = j.ccmasterid) pulled
from table1 j
INNER JOIN table3 s on j.ccstatus = s.sysstatusid and s.ccopenjob=false
where j.ccmasterid LIKE 'W%' and pulled = 0
这会产生一个错误:
ERROR: column "pulled" does not exist
LINE 6: where j.ccmasterid LIKE 'W%' and pulled = 0
如果我从查询中取出“and pulled = 0”,它的工作方式就像人们期望的那样从 table1 中生成一个记录列表,其中的值的总和表 2 拉。
ccmasterid pulled
W106063 0
W100553 9
W100685 1
我想不通的是如何 select 基于拉为 0。
将此查询更改为子查询,并将 WHERE 条件移动到外部查询:
SELECT * FROM (
Select j.ccmasterid,
(Select sum(i.ccmatpullqty) From table2 i
Where i.ccmasterid = j.ccmasterid) pulled
from table1 j
INNER JOIN table3 s on j.ccstatus = s.sysstatusid and s.ccopenjob=false
where j.ccmasterid LIKE 'W%'
) x
WHERE pulled = 0
避免在外部查询中针对 every 行而不是 once 运行的相关子查询,如果加入 [=12] 的聚合查询=] 子句:
SELECT j.ccmasterid
FROM table1 j
INNER JOIN table3 s
ON j.ccstatus = s.sysstatusid AND s.ccopenjob = false
INNER JOIN
(SELECT i.ccmasterid, SUM(i.ccmatpullqty) AS pulled
FROM table2 i
GROUP BY i.ccmasterid
) AS agg
ON agg.ccmasterid = j.ccmasterid
WHERE j.ccmasterid LIKE 'W%' AND agg.pulled = 0
甚至使用 CTE
WITH agg AS
(SELECT i.ccmasterid, SUM(i.ccmatpullqty) AS pulled
FROM table2 i
GROUP BY i.ccmasterid)
SELECT j.ccmasterid
FROM table1 j
INNER JOIN table3 s
ON j.ccstatus = s.sysstatusid AND s.ccopenjob = false
INNER JOIN agg
ON agg.ccmasterid = j.ccmasterid
WHERE j.ccmasterid LIKE 'W%' AND agg.pulled = 0
我正在尝试 select table1 中的项目,其中有一个子 table2 涉及第三个 table3。
Select j.ccmasterid,
(Select sum(i.ccmatpullqty) From table2 i
Where i.ccmasterid = j.ccmasterid) pulled
from table1 j
INNER JOIN table3 s on j.ccstatus = s.sysstatusid and s.ccopenjob=false
where j.ccmasterid LIKE 'W%' and pulled = 0
这会产生一个错误:
ERROR: column "pulled" does not exist LINE 6: where j.ccmasterid LIKE 'W%' and pulled = 0
如果我从查询中取出“and pulled = 0”,它的工作方式就像人们期望的那样从 table1 中生成一个记录列表,其中的值的总和表 2 拉。
ccmasterid pulled
W106063 0
W100553 9
W100685 1
我想不通的是如何 select 基于拉为 0。
将此查询更改为子查询,并将 WHERE 条件移动到外部查询:
SELECT * FROM (
Select j.ccmasterid,
(Select sum(i.ccmatpullqty) From table2 i
Where i.ccmasterid = j.ccmasterid) pulled
from table1 j
INNER JOIN table3 s on j.ccstatus = s.sysstatusid and s.ccopenjob=false
where j.ccmasterid LIKE 'W%'
) x
WHERE pulled = 0
避免在外部查询中针对 every 行而不是 once 运行的相关子查询,如果加入 [=12] 的聚合查询=] 子句:
SELECT j.ccmasterid
FROM table1 j
INNER JOIN table3 s
ON j.ccstatus = s.sysstatusid AND s.ccopenjob = false
INNER JOIN
(SELECT i.ccmasterid, SUM(i.ccmatpullqty) AS pulled
FROM table2 i
GROUP BY i.ccmasterid
) AS agg
ON agg.ccmasterid = j.ccmasterid
WHERE j.ccmasterid LIKE 'W%' AND agg.pulled = 0
甚至使用 CTE
WITH agg AS
(SELECT i.ccmasterid, SUM(i.ccmatpullqty) AS pulled
FROM table2 i
GROUP BY i.ccmasterid)
SELECT j.ccmasterid
FROM table1 j
INNER JOIN table3 s
ON j.ccstatus = s.sysstatusid AND s.ccopenjob = false
INNER JOIN agg
ON agg.ccmasterid = j.ccmasterid
WHERE j.ccmasterid LIKE 'W%' AND agg.pulled = 0