select 姓名
,sum(case when tab1.种类='a' then tab2.数量 else 0 end) a
,sum(case when tab1.种类='b' then tab2.数量 else 0 end) b
,sum(case when tab1.种类='c' then tab2.数量 else 0 end) c
from tab1 inner join tab2 on tab1.id=tab2.id
group by 姓名
,sum(case when tab1.种类='a' then tab2.数量 else 0 end) a
,sum(case when tab1.种类='b' then tab2.数量 else 0 end) b
,sum(case when tab1.种类='c' then tab2.数量 else 0 end) c
from tab1 inner join tab2 on tab1.id=tab2.id
group by 姓名
,sum(case when tab1.种类='a' then tab2.数量 else 0 end) a
,sum(case when tab1.种类='b' then tab2.数量 else 0 end) b
,sum(case when tab1.种类='c' then tab2.数量 else 0 end) c
from tab1 inner join tab2 on tab1.id=tab2.id
group by 姓名
SET @SQL='select 姓名 '
SELECT @SQL= @SQL+ ',sum(CASE WHEN tab1.种类 = ''' + 种类 + ''' THEN tab2.数量 else 0 END) [' + tab1.种类 + ']' FROM (SELECT DISTINCT tab1.种类 FROM tab1) A
SET @SQL=@SQL+' from tab1 inner join tab2 on tab1.id=tab2.id
group by 姓名 '
select @SQL
exec (@SQL)
Create table tab1
(id Int,
种类 Varchar(10))
Create table tab2
(姓名 Varchar(10),
种类 Int,
数量 Int)
GO
--插入数据
Insert tab1 Values(11, 'a')
Insert tab1 Values(22, 'b')
Insert tab1 Values(33, 'c')Insert tab2 Values('1', 11, 4)
Insert tab2 Values('1', 11, 5)
Insert tab2 Values('2', 22, 8)
Insert tab2 Values('3', 22, 5)
GO
--测试
DECLARE @SQL NVARCHAR(4000)
Set @SQL=N'select 姓名 '
SELECT @SQL= @SQL+ N',SUM(CASE WHEN tab1.种类 = ''' + 种类 + N''' THEN tab2.数量 else 0 END) '+ 种类 FROM (SELECT DISTINCT 种类 FROM tab1) A
SET @SQL=@SQL+N' From tab1 Inner Join tab2 On tab1.id=tab2.种类 Group By 姓名 '
exec (@SQL)
--删除测试环境
DROP table tab1,tab2
--结果
/*
姓名 a b c
1 9 0 0
2 0 8 0
3 0 5 0
*/
create table tb1 (id nvarchar(10),type nvarchar(10))
insert into tb1 select '11','a' union all select '22','b' union all select '33','c'create table tb2 (n int,type nvarchar(10),num int)
insert into tb2 select '1','11','4' union all select '1','11','5'
union all select '2','22','8' union all select '3','22','5'--查询处理
DECLARE @SQL VARCHAR(8000)
SET @SQL='select n '
SELECT @SQL= @SQL+',sum(case when type='+ttt+' then num else 0 end)['+tt+']' from
(select distinct a.type as tt,isnull(b.type,'0') as ttt from tb2 b right join tb1 a on a.id=b.type) bset @sql=@sql+' from tb2 group by n'
print @sql
exec(@sql)
go
--删除测试环境
Drop Table tb1,tb2
(id Int,
种类 Varchar(10))
Create table #tab2
(姓名 Varchar(10),
种类 Int,
数量 Int)
GO
--插入数据
Insert #tab1 Values(11, 'a')
Insert #tab1 Values(22, 'b')
Insert #tab1 Values(33, 'c')Insert #tab2 Values('1', 11, 4)
Insert #tab2 Values('1', 11, 5)
Insert #tab2 Values('2', 22, 8)
Insert #tab2 Values('3', 22, 5)
GO
declare @str varchar(8000)
set @str=''
select @str=@str+','+a.种类 +'=sum(case a.种类 when '+''''+a.种类+''''+' then 数量 else 0 end )' from #tab1 a left join #tab2 b on a.id=b.种类 group by a.种类
set @str='select 姓名 '+@str+' from #tab1 a inner join #tab2 b on a.id=b.种类 group by a.种类,姓名'
exec(@str)
,sum(case when tab1.种类='a' then tab2.数量 else 0 end) a
,sum(case when tab1.种类='b' then tab2.数量 else 0 end) b
,sum(case when tab1.种类='c' then tab2.数量 else 0 end) c
from tab1 inner join tab2 on tab1.id=tab2.id
group by 姓名
,sum(case when tab1.种类='a' then tab2.数量 else 0 end) a
,sum(case when tab1.种类='b' then tab2.数量 else 0 end) b
,sum(case when tab1.种类='c' then tab2.数量 else 0 end) c
from tab1 inner join tab2 on tab1.id=tab2.id
group by 姓名
sum(case when tab1.种类='a' then tab2.数量 else 0 end) a
,sum(case when tab1.种类='b' then tab2.数量 else 0 end) b
,sum(case when tab1.种类='c' then tab2.数量 else 0 end) c
from tab1 inner join tab2 on tab1.id=tab2.种类
group by 姓名
declare @sql_colvalue as varchar(30)
declare @sql_colcount intselect @sql_colcount=count(distinct 种类) from tab1 --找出子表最大的项数select @sql_exe='' declare cur_colname cursor
for
select distinct 种类 from tab1 --查找的记录 /*打开游标*/
open cur_colname
fetch next from cur_colname
into @sql_colvalue
while (@@fetch_status=0)
begin
select @sql_exe=@sql_exe+',sum(case tab1.种类 when '+char(39)+@sql_colvalue+char(39)+' then 数量 else 0 end) '+char(39)+@sql_colvalue+char(39) --用语句构造列 /*游标指向下一条记录*/
fetch next from cur_colname
into @sql_colvalue
end
close cur_colname
deallocate cur_colnameselect @sql_exe='select 姓名'+@sql_exe+' from tab1,tab2 where tab1.id=tab2.种类'if @sql_colcount=0
select message='no records'
else
execute(@sql_exe)
select @sql_exe='select 姓名'+@sql_exe+' from tab1,tab2 where tab1.id=tab2.种类'改成
select @sql_exe='select 姓名'+@sql_exe+' from tab1,tab2 where tab1.id=tab2.种类 group by 姓名'