SELECT b.nazv,sum(b.kol),sum(b.summa)
FROM (SELECT SUPERMAG.SMCASHCHECKITEMS.CHECKNUM as num,
SUPERMAG.SMCASHCHECKITEMS.DESKNUM as kassa,
SUPERMAG.SMCASHCHECKITEMS.ZNUM as zet,
SUPERMAG.SMCARD.NAME as nazv,
SUPERMAG.SMCASHCHECKITEMS.QUANTITY as kol,
SUPERMAG.SMCASHCHECKITEMS.TOTALSUM as summa
FROM SUPERMAG.SMCARD,SUPERMAG.SMCASHCHECKITEMS
WHERE (SUPERMAG.SMCARD.NAME LIKE '%Государь%'
OR SUPERMAG.SMCARD.NAME LIKE '%госуд%')
AND (SUPERMAG.SMCASHCHECKITEMS.ARTICLE=SUPERMAG.SMCARD.ARTICLE)) as b,
SUPERMAG.SMCASHCHECKS
WHERE (b.num = SUPERMAG.SMCASHCHECKS.CHECKNUM)
AND (b.kassa = SUPERMAG.SMCASHCHECKS.DESKNUM)
AND (b.zet = SUPERMAG.SMCASHCHECKS.ZNUM)
AND SUPERMAG.SMCASHCHECKS.PRINTTIME>='01.05.2009'
AND SUPERMAG.SMCASHCHECKS.PRINTTIME<='31.05.2009'
SELECT B.NAZV, SUM(B.KOL), SUM(B.SUMMA) FROM (SELECT SUPERMAG.SMCASHCHECKITEMS.CHECKNUM AS NUM, SUPERMAG.SMCASHCHECKITEMS.DESKNUM AS KASSA, SUPERMAG.SMCASHCHECKITEMS.ZNUM AS ZET, SUPERMAG.SMCARD.NAME AS NAZV, SUPERMAG.SMCASHCHECKITEMS.QUANTITY AS KOL, SUPERMAG.SMCASHCHECKITEMS.TOTALSUM AS SUMMA FROM SUPERMAG.SMCARD, SUPERMAG.SMCASHCHECKITEMS WHERE (SUPERMAG.SMCARD.NAME LIKE '%Государь%' OR SUPERMAG.SMCARD.NAME LIKE '%госуд%') AND (SUPERMAG.SMCASHCHECKITEMS.ARTICLE = SUPERMAG.SMCARD.ARTICLE)) B, SUPERMAG.SMCASHCHECKS WHERE (B.NUM = SUPERMAG.SMCASHCHECKS.CHECKNUM) AND (B.KASSA = SUPERMAG.SMCASHCHECKS.DESKNUM) AND (B.ZET = SUPERMAG.SMCASHCHECKS.ZNUM) AND SUPERMAG.SMCASHCHECKS.PRINTTIME >= '01.05.2009' AND SUPERMAG.SMCASHCHECKS.PRINTTIME <= '31.05.2009' GROUP BY B.NAZV
select z1.clientindex, z1.name, sum(z2.prodsum) from (SELECT DISTINCT dc.article, d.clientindex, n.name FROM supermag.smdocuments d, supermag.smspec dc, supermag.smclientinfo n WHERE d.doctype=dc.doctype AND d.id=dc.docid AND d.clientindex=n.id AND d.opcode=0 AND d.docstate>=2 AND TO_DATE(d.createdat) >= ('01.01.2009'))z1, (SELECT dc.article, sum(dc.quantity), sum(dc.totalprice) as prodsum FROM supermag.smdocuments d, supermag.smspec dc WHERE d.opcode=1 AND d.docstate = 3 AND TO_DATE(d.createdat) BETWEEN '01.04.09' AND '30.04.09' AND dc.doctype = d.doctype AND dc.docid = d.id GROUP BY dc.article ORDER BY dc.article) z2 where z1.article = z2.article group by z1.clientindex, z1.name