Postgresql无法使用字段和来过滤查询

时间:2017-03-27 18:32:16

标签: postgresql sum where-clause

实际上我需要获得所有具有SUM(行)>的行。 0,但是当我创建句子并执行它时:

select o.corden,
    o.cproveedor,
    o.fecha_orden,
    o.status,
    o.monto_total,
    SUM(od.cant_restante) as cant_pend,
    COUNT(od.cart_comercial) as articulos,
    d.nombre as proveedor
from ordencompra o
inner join ordencompra_det od on o.corden = od.corden_det
inner join proveedores p on o.cproveedor = p.cproveedor
inner join data d on p.cdata = d.cdata
where o.status = 'PROCESADA'
    and cant_pend > 0
group by o.corden,
    d.nombre
order by o.corden asc

它说:列<< cant_pend>>不存在! 所以我把它改成:

select o.corden,
    o.cproveedor,
    o.fecha_orden,
    o.status,
    o.monto_total,
    SUM(od.cant_restante) as cant_pend,
    COUNT(od.cart_comercial) as articulos,
    d.nombre as proveedor
from ordencompra o
inner join ordencompra_det od on o.corden = od.corden_det
inner join proveedores p on o.cproveedor = p.cproveedor
inner join data d on p.cdata = d.cdata
where o.status = 'PROCESADA'
    and SUM(od.cant_restante) > 0
group by o.corden,
    d.nombre
order by o.corden asc

agregate功能不允许在clausule

的地方

对句子有任何帮助吗?

1 个答案:

答案 0 :(得分:1)

您想要having子句:

select o.corden,
    o.cproveedor,
    o.fecha_orden,
    o.status,
    o.monto_total,
    SUM(od.cant_restante) as cant_pend,
    COUNT(od.cart_comercial) as articulos,
    d.nombre as proveedor
from ordencompra o
inner join ordencompra_det od on o.corden = od.corden_det
inner join proveedores p on o.cproveedor = p.cproveedor
inner join data d on p.cdata = d.cdata
where o.status = 'PROCESADA'
group by o.corden,
    d.nombre
having SUM(od.cant_restante) > 0
order by o.corden asc