日期:2014-05-19  浏览次数:20669 次

多表查询
有20个表,数据类型,结构完全相同
a01     产品名称,数量
b01   产品名称,数量
c01   产品名称,数量
d01   产品名称,数量
.
.
.

想要用语句实现每个表的累计数量
a01           b01           c01         d01
1000         500           3000         444




------解决方案--------------------
declare @tab varchar(20)
set @tab= '表名 '
print( 'select sum(case when col1= ' 'a01 ' ' then col3 else 0 end)a01,
sum(case when col1= ' 'a01 ' ' then col3 else 0 end)b01,
sum(case when col1= ' 'a01 ' ' then col3 else 0 end)c01,
sum(case when col1= ' 'a01 ' ' then col3 else 0 end)d01
from '+@tab+ ' group by col1 ')
------解决方案--------------------
select
(select Sum(数量) as a01 from a01)a
Cross join
(select Sum(数量) as b01 from b01)b
Cross join
(select Sum(数量) as c01 from c01)c
Cross join
(select Sum(数量) as d01 from d01)d