在论坛中出现的比较难的sql问题:15(生成动态删除列语句 分组内多行转为多列)

最近,在论坛中,遇到了不少比较难的sql问题,虽然自己都能解决,但发现过几天后,就记不起来了,也忘记解决的方法了。

所以,觉得有必要记录下来,这样以后再次碰到这类问题,也能从中获取解答的思路。


1、如果去掉这个临时表中合计为0 的字段
http://bbs.csdn.net/topics/390625348

我有一个临时表 ##temp,

字段 
住院号,床位,应收金额,优惠金额1,优惠金额2,优惠金额3,优惠金额4.。。。。优惠金额N

 我想把临时表中 优惠金额X 合计为0的字段去掉,如何去?
 又或者,生成另一个没有 优惠金额X 合计为0字段的临时表。


我的解法:

--		drop table ##temp

create table ##temp
(
住院号 varchar(20),
床位 varchar(20),
应收金额 numeric(20,3),
优惠金额1 numeric(20,3),
优惠金额2 numeric(20,3),
优惠金额3 numeric(20,3),
优惠金额4 numeric(20,3)
)

insert into ##temp
select '00000','111',1000, 0,0,0,10 union all
select '00001','112',1000, 0 ,0,0,0 union all
select '00002','113',1000, 0,0,0,0 union all
select '00003','114',1000, 0 ,0,0,20 union all
select '00004','115',1000, 0,2,0,3 union all
select '00005','116',1000, 0,0,0,0 union all
select '00006','117',1000, 0,0,0,0 

go
	
declare @sql nvarchar(max);
declare @sql_delete_column nvarchar(max);

declare @tb table(column_name nvarchar(100),rownum int)
declare @count int;
declare @i int;
declare @return int;

declare @temp_name nvarchar(100);
declare @del_column nvarchar(100);

set @sql = '';
set @sql_delete_column  = '';

--临时表名
set @temp_name = '##temp'

--需要删除的列名
set @del_column = '%优惠金额%';


insert into @tb
select --t.name,
       c.name as column_name,
       row_number() over(order by @@servername) as rownum
       --c.column_id
from tempdb.sys.tables t
inner join tempdb.sys.columns c
        on t.object_id = c.object_id
where t.name = @temp_name         
      and c.name like @del_column;


set @count = (select count(*) from @tb);
set @i = 1;

while @i <= @count
begin
	set @sql = 'select @return=sum('+ 
	           (select column_name from @tb where rownum = @i) +
	           ') from ' + @temp_name;
	    
	exec sp_executesql @sql,N'@return int output',@return output;
	
	select @sql_delete_column = 
	          @sql_delete_column +
	          case when @return <> 0 then ' '
	                    else 'alter table '+@temp_name +
	                         ' drop column '+
	                         (select column_name from @tb where rownum = @i) +
	                         ';'
	               end
    set @i = @i +1 
end


--动态生成的删除列语句
select @sql_delete_column
/*
(无列名)
alter table ##temp drop column 优惠金额1; 
alter table ##temp drop column 优惠金额3; 
*/


--删除列
exec(@sql_delete_column)


--查询数据
select * from ##temp;
/*
住院号	床位	应收金额	优惠金额2	优惠金额4
00000	111	1000.000	0.000	10.000
00001	112	1000.000	0.000	0.000
00002	113	1000.000	0.000	0.000
00003	114	1000.000	0.000	20.000
00004	115	1000.000	2.000	3.000
00005	116	1000.000	0.000	0.000
00006	117	1000.000	0.000	0.000
*/


2、动态行转列
http://bbs.csdn.net/topics/390646474
型号  年 月 日 准确率 缺到率 可用率
thd 2013 1 1  56   23    34
thd 2013 1 1  66   77    54
thd 2013 1 1  78   55    77
hhh 2012 9 18 89   55    23
hhn 2012 9 18 33   37    45
hhn 2012 9 18 67   56    12

上面的数据 怎样变成下面这样
即怎样将同一天同一型号的数据在一行显示

型号  年 月 日 准确率 缺到率 可用率 准确率 缺到率 可用率 准确率 缺到率 可用率
thd 2013 1 1  56   23    34    66   77    54     78   55    77
hhh 2012 9 18 89   55    23    33   37    45     67   56    12


我的解法:
drop table tb
go

create table tb(
型号 varchar(20),年 int, 月 int, 日 int, 
准确率 int, 缺到率 int,可用率 int
)

insert into tb
select 'thd' ,2013, 1, 1 , 56 ,  23  ,  34
union all select 'thd', 2013 ,1 ,1  ,66   ,77    ,54
union all select 'thd', 2013 ,1 ,1  ,78   ,55    ,77
union all select 'hhh', 2012 ,9 ,18 ,89   ,55    ,23
union all select 'hhh', 2012 ,9 ,18 ,33   ,37    ,45
union all select 'hhh', 2012 ,9 ,18 ,67   ,56    ,12
go


declare @sql nvarchar(max);

set @sql = '';

;with t
as
(
select *,
       ROW_NUMBER() over(partition by 型号,年,月,日 order by @@servername) as rownum
from tb
)

select 
       @sql = @sql + ',max(case when rownum = '+cast(rownum as varchar)+' then 准确率 else null end) as 准确率' +
                     ',max(case when rownum = '+cast(rownum as varchar)+' then 缺到率 else null end) as 缺到率' +
                     ',max(case when rownum = '+cast(rownum as varchar)+' then 可用率 else null end) as 可用率'
from t
group by rownum


select @sql = 'select 型号,年,月,日' + @sql + 
              ' from (select *,
                            ROW_NUMBER() over(partition by 型号,年,月,日 order by @@servername) as rownum
                     from tb)t' +
              ' group by 型号,年,月,日'
              
--select @sql

exec(@sql)
/*
型号	年	月	日	准确率	缺到率	可用率	准确率	缺到率	可用率	准确率	缺到率	可用率
hhh	2012	9	18	89	55	23	33	37	45	67	56	12
thd	2013	1	1	56	23	34	66	77	54	78	55	77
*/

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值