Answer the question
In order to leave comments, you need to log in
Is there an error in the request?
Hello)
When you run a query
with rec(pr.pid,cid,par_cid,name,incoming,outcoming,balance) as (
select
pr.pid,
null as cid,
pr.rcid as par_cid,
pr.pname as name,
sum(case when incoming = '1' then rec.quantity end) incoming,
sum(case when incoming = '0' then rec.quantity end) outcoming,
sum(case when incoming = '1' then 1 else -1 end * rec.quantity) balance
from
products pr
inner join records rec on rec.rpid=pr.pid
group by
pr.pid,
pr.rcid,
pr.pname
union all select
null,
cat.cid,
cat.par_cid,
cat.rname,
incoming,
outcoming,
balance
from
rec
inner join catalog cat on cat.cid = rec.par_cid)
select
pid,
cid,
name,
sum(incoming),
sum(outcoming),
sum(balance)
from
rec
group by
pid,
cid,
name;
Answer the question
In order to leave comments, you need to log in
Add brackets:
sum((case when incoming = '1' then 1 else -1 end) * rec.quantity) balance
The first thing that catches your eye is that the select contains fields that do not participate in either aggregation or grouping.
select
pr.pid,
null as cid,
pr.rcid as par_cid,
pr.pname as name,
sum(case when incoming = '1' then rec.quantity end) incoming,
sum(case when incoming = '0' then rec.quantity end) outcoming,
sum(case when incoming = '1' then 1 else -1 end * rec.quantity) balance
from
products pr
inner join records rec on rec.rpid=pr.pid
group by
pr.pid,
pr.rcid,
pr.pname
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question