select sp.article, sp.doctype, sp.docid, case when trim(sp.doctype) = 'CS' then sp.quantity when trim(sp.doctype) = 'CR' then -sp.quantity end as "Количество", sp.totalprice from smspec sp, smcard crd, smdocuments s where sp.doctype IN ('CS', 'CR') and s.id=sp.docid and sp.doctype = s.doctype and s.createdat between '&date1' and '&date2' and sp.article = crd.article and crd.idclass in (select idclass from sacardclass where tree like '1.%')
select sum(quantity) from
(select sp.article, sp.doctype, sp.docid,
case
when trim(sp.doctype) = 'CS' then sp.quantity
when trim(sp.doctype) = 'CR' then -sp.quantity
end as quantity, sp.totalprice
from smspec sp, smcard crd, smdocuments s
where sp.doctype IN ('CS', 'CR')
and s.id=sp.docid
and sp.doctype = s.doctype
and s.createdat between '&date1' and '&date2'
and sp.article = crd.article
and crd.idclass in (select idclass from sacardclass where tree like '1.%')) p
select sp.article, sp.doctype, sp.docid,
case
when trim(sp.doctype) = 'CS' then sp.quantity
when trim(sp.doctype) = 'CR' then -sp.quantity
end as quantity, sp.totalprice, sum(quantity)
from smspec sp, smcard crd, smdocuments s
where sp.doctype IN ('CS', 'CR')
and s.id=sp.docid
and sp.doctype = s.doctype
and s.createdat between '&date1' and '&date2'
and sp.article = crd.article
and crd.idclass in (select idclass from sacardclass where tree like '1.%')
select sp.article, sum(case when trim(sp.doctype) = 'CS' then sp.quantity when trim(sp.doctype) = 'CR' then -sp.quantity end ) as quantity, sum(case when trim(sp.doctype) = 'CS' then sp.totalprice when trim(sp.doctype) = 'CR' then -sp.totalprice end) totalprice from smspec sp, smcard crd, smdocuments s where sp.doctype IN ('CS', 'CR') and s.id=sp.docid and sp.doctype = s.doctype and s.createdat between '&date1' and '&date2' and sp.article = crd.article and crd.idclass in (select idclass from sacardclass where tree like '1.%') group by sp.article
select sp.article, decode(sp.doctype,'CS', sp.locationfrom, sp.locationto), sum(case when trim(sp.doctype) = 'CS' then sp.quantity when trim(sp.doctype) = 'CR' then -sp.quantity end ) as quantity, sum(case when trim(sp.doctype) = 'CS' then sp.totalprice when trim(sp.doctype) = 'CR' then -sp.totalprice end) totalprice from smspec sp, smcard crd, smdocuments s where sp.doctype IN ('CS', 'CR') and s.id=sp.docid and sp.doctype = s.doctype and s.createdat between '&date1' and '&date2' and sp.article = crd.article and crd.idclass in (select idclass from sacardclass where tree like '1.%') group by sp.article, decode(sp.doctype,'CS', sp.locationfrom, sp.locationto)
select sp.article,
sum(case
when trim(sp.doctype) = 'CS' then sp.quantity
when trim(sp.doctype) = 'CR' then -sp.quantity
end ) as quantity,
sum(case
when trim(sp.doctype) = 'CS' then sp.totalprice
when trim(sp.doctype) = 'CR' then -sp.totalprice
end) as totalprice
from smspec sp, smcard crd, smdocuments s
where sp.doctype IN ('CS', 'CR')
and s.id=sp.docid
and sp.doctype = s.doctype
and (s.createdat between '&date1' and '&date2' and quantity < '&quantity')
and sp.article = crd.article
and crd.idclass in (select idclass from sacardclass where tree like '1.%')
group by sp.article
sum(case when trim(sp.doctype) = 'CS' then sp.quantity when trim(sp.doctype) = 'CR' then -sp.quantity end ) as quantity
select p.article, p.quant, p.total_$ from (select sp.article, sum(case when trim(sp.doctype) = 'CS' then sp.quantity when trim(sp.doctype) = 'CR' then -sp.quantity end ) as quant, sum(case when trim(sp.doctype) = 'CS' then sp.totalprice when trim(sp.doctype) = 'CR' then -sp.totalprice end) as total_$ from smspec sp, smcard crd, smdocuments s where sp.doctype IN ('CS', 'CR') and s.id=sp.docid and sp.doctype = s.doctype and (s.createdat between '&date1' and '&date2') and sp.article = crd.article and crd.idclass in (select idclass from sacardclass where tree like '1.%') group by sp.article) p where p.quant < 5 order by 1