我有2个表管和泄漏
Pipe(Pipe_object, Pipe_status, DIAMETER, MATERIAL, BBZ_LEVEL, SUR_REQ_DATE, PRESS_REG)
Leakage (Pipe_object,LEAKAGE_DATE, Job_No, Leak_Cause, Rep_Type)
我必须找到leak_cause ='ST'和sur_req_date不为空的记录。当前下面的查询显示的是具有计数< = 2的记录(即如果条件指定为2则显示3个pipe_objects)
SELECT PI.PIPE_OBJECT, PI.PRESS_REG,PI.DIAMETER,PI.MATERIAL,PI.PIPE_STATUS, PI.BBZ_LEVEL, LE.PIPE_OBJECT, LE.LEAKAGE_DATE
FROM PIPE PI JOIN LEAKAGE LE ON PI.PIPE_OBJECT = LE.PIPE_OBJECT
WHERE PI.MATERIAL = 'CI' AND PI.SUR_REQ_DATE IS NOT NULL AND PI.PIPE_STATUS <> 'A' AND PRESS_REG = 'LP'
AND LE.LEAK_CAUSE = 'ST' AND BBZ_LEVEL IN ('1','2','3','4','5') AND PI.DIAMETER = 6 AND PI.LDZ_ID = 'SC'
GROUP BY PI.PIPE_OBJECT,PI.PRESS_REG, PI.BBZ_LEVEL, LE.LEAK_CAUSE, PI.PIPE_STATUS,LE.PIPE_OBJECT,
LE.LEAKAGE_DATE, PI.MATERIAL, PI.DIAMETER HAVING COUNT(LE.PIPE_OBJECT) <= 2;