我在下面复制的查询中收到以下错误:
聚合不能出现在ON子句中,除非它位于HAVING子句或选择列表中包含的子查询中,并且要聚合的列是外部引用。
由于d.VCMN和h.VDMN上没有1对1的连接,我想加入d.VCMN< = max(h.VDMN)来获取该h.P_ID,b.S_U_ID
如何为这种情况设置SQL?
SELECT DISTINCT H.P_ID
,B.S_U_ID
,H.vdmn
,D.VCMN AS VCMN _d
,C.I_A_ID
,D.I_A_V_ID
FROM PSH H
LEFT JOIN PSU B ON h.SH_ID = B.SH_ID
LEFT JOIN PPA C ON b.UA_ID = C.PA_ID
LEFT JOIN PIA_V D ON c.IIA_ID = d.IA_ID
AND D.VCMN <= max(H.VDMN)
答案 0 :(得分:0)
也许你想要这样的事情:
From (select h.*, max(vdmn) over (partition by sh_id) as max_vdmn
from PSH H
) h left join
PSU B
on h.SH_ID = B.SH_ID left join
PPA C
on b.UA_ID = C.PA_ID left join
PIA_V D
on c.IIA_ID = d.IA_ID and D.VCMN <= h.max_vdmn
partition by sh_id
只是猜测你可能想要完成的事情。某些partition by
可能会解决此特定语法问题。