22,219
社区成员
发帖
与我相关
我的任务
分享
if object_id('tb') is not null drop table tb
create table tb(company_id nvarchar(10),bank_id nvarchar(20),amount money)
insert tb
select '1001','中行',100.00 union all
select '1002','建行',200.00 union all
select '1002','建行',300.00 union all
select '1001','招行',400.00
--动态SQL(2005)。
declare @CMD varchar(8000),@CMD1 varchar(8000)
select @CMD = isnull(@CMD + '],[' , '') + bank_id from tb group by bank_id
set @CMD ='[' + @CMD + ']'
exec ('select * from (select company_id, bank_id,amount from tb) a PIVOT(sum(amount) for bank_id in ('+@CMD+')) b')
/*
company_id 建行 招行 中行
---------- --------------------- --------------------- ---------------------
1001 NULL 400.00 100.00
1002 500.00 NULL NULL
(2 行受影响)
*/
select company_id ,
sum(case bank_id when '建行' then amount else 0 end) [建行] ,
sum(case bank_id when '招行' then amount else 0 end) [招行] ,
sum(case bank_id when '中行' then amount else 0 end) [中行]
from tb group by company_id
create table tb(company_id int, bank_id varchar(10), amount decimal(18,2))
go
insert tb select 1001 , '中行' , 100.00
insert tb select 1002 , '建行' , 200.00
insert tb select 1002 , '建行' , 300.00
insert tb select 1001 , '招行', 400.00
go
declare @sql varchar(8000)
set @sql = 'select company_id '
select @sql = @sql + ' , sum(case bank_id when ''' + ltrim(bank_id) + ''' then amount else 0 end) [' + ltrim(bank_id) + ']'
from (select distinct bank_id from tb) as a
set @sql = @sql + ' from tb group by company_id '
print @sql
exec(@sql)
go
drop table tb
go
/*
company_id 建行 招行 中行
----------- ---------------------------------------- ---------------------------------------- ----------------------------------------
1001 .00 400.00 100.00
1002 500.00 .00 .00
*/
create table tb(company_id varchar(10), bank_id varchar(10), amount decimal(18,2))
go
insert tb select 1001 , '中行' , 100.00
insert tb select 1002 , '建行' , 200.00
insert tb select 1002 , '建行' , 300.00
insert tb select 1001 , '招行', 400.00
go
declare @sql varchar(8000)
set @sql = 'select company_id '
select @sql = @sql + ' , max(case bank_id when ''' + ltrim(bank_id) + ''' then amount else 0 end) [' + ltrim(bank_id) + ']'
from (select distinct bank_id from tb) as a
set @sql = @sql + ' from tb group by company_id '
exec(@sql)
go
drop table tb
go
/*
company_id 建行 招行 中行
---------- -------------------- -------------------- --------------------
1001 .00 400.00 100.00
1002 300.00 .00 .00
*/
create table tb(company_id int, bank_id varchar(10), amount decimal(18,2))
insert into tb values(1001 , '中行' , 100.00 )
insert into tb values(1002 , '建行' , 200.00 )
insert into tb values(1002 , '建行' , 300.00 )
insert into tb values(1001 , '招行' , 400.00 )
go
declare @sql varchar(8000)
set @sql = 'select company_id '
select @sql = @sql + ' , sum(case bank_id when ''' + bank_id + ''' then amount else 0 end) [' + bank_id + ']'
from (select distinct bank_id from tb) as a
set @sql = @sql + ' from tb group by company_id'
exec(@sql)
drop table tb
/*
company_id 建行 招行 中行
----------- ---------------------------------------- ---------------------------------------- ----------------------------------------
1001 .00 400.00 100.00
1002 500.00 .00 .00
*/
declare @sql varchar(8000)
set @sql='select company_id'
select @sql=@sql+',['+bank_id+']=sum(case bank_id when '''+bank_id+''' then amount esle 0 end)'
from (select distinct bank_id from 表) t
set @sql=@sql+' from 表 group by company_id'
exec(@sql)
declare @sql varchar(8000)
set @sql = 'select company_id '
select @sql = @sql + ' , max(case bank_id when ''' + ltrim(bank_id) + ''' then amount else 0 end) [' + ltrim(bank_id) + ']'
from (select distinct bank_id from tb) as a
set @sql = @sql + ' from tb group by company_id '
exec(@sql)
declare @sql varchar(8000)
set @sql = 'select company_id '
select @sql = @sql + ' , sum(case bank_id when ''' + bank_id + ''' then amount else 0 end) [' + bank_id + ']'
from (select distinct bank_id from tb) as a
set @sql = @sql + ' from tb group by company_id'
exec(@sql)