1

actually I need get all the rows that have a SUM(row) > 0, but when I make the sentence and execute it:

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

it said: The column << cant_pend >> doesn't exist! So i change it to:

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 function's can't be allowed on where clausule

Any help with the sentence?

1 Answer 1

2

You want having clause:

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
Sign up to request clarification or add additional context in comments.

9 Comments

Is exactly what i just need it! Thanks :)
Any difference between have the sql queries in mayus or minus?
@JuJoGuAl - Sorry? I didn't understand what you just asked.
Any difference between have the sql queries in uppercase or lowercase* ?
Depend on programer. I put all keywords in UPPER CASE rest is lower case to make it easy to read. But doesnt make different to the db
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.