22,199
社区成员
发帖
与我相关
我的任务
分享
select id,sum(num) from (
select id,sum(num) as num from ta group by id
union all
select id,sum(num) from tb group by id
)p
group by id
[/quote][/quote]
两猫眼睁那么大,光看凶器了吧[/quote]select id,sum(num) from (
select id,sum(num) as num from ta group by id
union all
select id,sum(num) from tb group by id
)p
group by id
[/quote][/quote]
两猫眼睁那么大,光看凶器了吧select id,sum(num) from (
select id,sum(num) as num from ta group by id
union all
select id,sum(num) from tb group by id
)p
group by id
[/quote]select id,sum(num) from (
select id,sum(num) as num from ta group by id
union all
select id,sum(num) from tb group by id
)p
group by id
CREATE TABLE #t (id int)
SELECT * FROM #t
--ORDER BY id
UNION ALL
SELECT * FROM #t
ORDER BY id
CREATE TABLE #t (id int)
SELECT * FROM #t
ORDER BY id
UNION ALL
SELECT * FROM #t
ORDER BY id
/*
Msg 156, Level 15, State 1, Line 4
Incorrect syntax near the keyword 'UNION'.
*/
select p.billno,p.fitemid ,sum(p.fqty) as fqty from (
select t.fbillno as billno,t1.fitemid,sum(t1.fqty) as fqty from icstockbill t left join icstockbillentry t1 on t1.finterid=t.finterid
inner join (select distinct left(fbillno,CHARINDEX('退',fbillno)-1) as fbillno from SEOutStock where fbillno like '%退%' and fdate>='2008-01-01' and fdate<='2013-12-31' and fcancellation=0 and fstatus>=1 group by fbillno) o on o.fbillno = t.fbillno
where t.fdate>='2008-01-01' and t.fdate<='2013-12-31' and ftrantype=21 and frob=1 and fcancellation=0 and fstatus>=1
group by t.fbillno,t1.fitemid
union all
select left(tp.fbillno,CHARINDEX('退',tp.fbillno)-1) as billno,tp1.fitemid,sum(tp1.fqty) as fqty from icstockbill tp left join icstockbillentry tp1 on tp1.finterid=tp.finterid
where tp.fbillno like '%退%' and tp.fdate>='2008-01-01' and tp.fdate<='2013-12-31' and ftrantype=21 and fcancellation=0 and fstatus>=1
group by tp.fbillno,tp1.fitemid
union all
select tt1.fsourcebillno as billno,fitemid,sum(fqty) as fqty from icstockbill tt left join icstockbillentry tt1 on tt1.finterid=tt.finterid
inner join (select distinct left(fbillno,CHARINDEX('退',fbillno)-1) as fbillno from SEOutStock where fbillno like '%退%' and fdate>='2008-01-01' and fdate<='2013-12-31' and fcancellation=0 and fstatus>=1 group by fbillno) o1 on o1.fbillno = tt1.fsourcebillno
where tt.fdate>='2008-01-01' and tt.fdate<='2013-12-31' and ftrantype=21 and frob=-1 and fcancellation=0 and fstatus>=1
group by tt1.fsourcebillno,tt1.fitemid
) p
group by p.billno,p.fitemid
select *
from (
select *,1 as orderid
from tb
union all
select *,2
from tb2
union all
select *,3
from tb3)x
order by orderid