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

来源:互联网 发布:javascript书籍 知乎 编辑:程序博客网 时间:2024/06/14 11:26
最近,在论坛中,遇到了不少比较难的sql问题,虽然自己都能解决,但发现过几天后,就记不起来了,也忘记解决的方法了。

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


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

我有一个临时表 ##temp,

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

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


我的解法:

--drop table ##tempcreate 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 ##tempselect '00000','111',1000, 0,0,0,10 union allselect '00001','112',1000, 0 ,0,0,0 union allselect '00002','113',1000, 0,0,0,0 union allselect '00003','114',1000, 0 ,0,0,20 union allselect '00004','115',1000, 0,2,0,3 union allselect '00005','116',1000, 0,0,0,0 union allselect '00006','117',1000, 0,0,0,0 godeclare @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 @tbselect --t.name,       c.name as column_name,       row_number() over(order by @@servername) as rownum       --c.column_idfrom tempdb.sys.tables tinner join tempdb.sys.columns c        on t.object_id = c.object_idwhere t.name = @temp_name               and c.name like @del_column;set @count = (select count(*) from @tb);set @i = 1;while @i <= @countbeginset @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优惠金额4000001111000.0000.00010.000000011121000.0000.0000.000000021131000.0000.0000.000000031141000.0000.00020.000000041151000.0002.0003.000000051161000.0000.0000.000000061171000.0000.0000.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 tbgocreate table tb(型号 varchar(20),年 int, 月 int, 日 int, 准确率 int, 缺到率 int,可用率 int)insert into tbselect 'thd' ,2013, 1, 1 , 56 ,  23  ,  34union all select 'thd', 2013 ,1 ,1  ,66   ,77    ,54union all select 'thd', 2013 ,1 ,1  ,78   ,55    ,77union all select 'hhh', 2012 ,9 ,18 ,89   ,55    ,23union all select 'hhh', 2012 ,9 ,18 ,33   ,37    ,45union all select 'hhh', 2012 ,9 ,18 ,67   ,56    ,12godeclare @sql nvarchar(max);set @sql = '';;with tas(select *,       ROW_NUMBER() over(partition by 型号,年,月,日 order by @@servername) as rownumfrom 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 tgroup by rownumselect @sql = 'select 型号,年,月,日' + @sql +               ' from (select *,                            ROW_NUMBER() over(partition by 型号,年,月,日 order by @@servername) as rownum                     from tb)t' +              ' group by 型号,年,月,日'              --select @sqlexec(@sql)/*型号年月日准确率缺到率可用率准确率缺到率可用率准确率缺到率可用率hhh2012918895523333745675612thd201311562334667754785577*/