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

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

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


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

我有一个临时表 ##temp,

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

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


我的解法:


   
   
  1. -- drop table ##temp
  2. create table ##temp
  3. (
  4. 住院号 varchar( 20),
  5. 床位 varchar( 20),
  6. 应收金额 numeric( 20, 3),
  7. 优惠金额 1 numeric( 20, 3),
  8. 优惠金额 2 numeric( 20, 3),
  9. 优惠金额 3 numeric( 20, 3),
  10. 优惠金额 4 numeric( 20, 3)
  11. )
  12. insert into ##temp
  13. select '00000', '111', 1000, 0, 0, 0, 10 union all
  14. select '00001', '112', 1000, 0 , 0, 0, 0 union all
  15. select '00002', '113', 1000, 0, 0, 0, 0 union all
  16. select '00003', '114', 1000, 0 , 0, 0, 20 union all
  17. select '00004', '115', 1000, 0, 2, 0, 3 union all
  18. select '00005', '116', 1000, 0, 0, 0, 0 union all
  19. select '00006', '117', 1000, 0, 0, 0, 0
  20. go
  21. declare @ sql nvarchar( max);
  22. declare @sql_delete_column nvarchar( max);
  23. declare @tb table(column_name nvarchar( 100), rownum int)
  24. declare @ count int;
  25. declare @i int;
  26. declare @ return int;
  27. declare @temp_name nvarchar( 100);
  28. declare @del_column nvarchar( 100);
  29. set @ sql = '';
  30. set @sql_delete_column = '';
  31. --临时表名
  32. set @temp_name = '##temp'
  33. --需要删除的列名
  34. set @del_column = '%优惠金额%';
  35. insert into @tb
  36. select --t.name,
  37. c.name as column_name,
  38. row_number() over( order by @@servername) as rownum
  39. --c.column_id
  40. from tempdb.sys.tables t
  41. inner join tempdb.sys.columns c
  42. on t.object_id = c.object_id
  43. where t.name = @temp_name
  44. and c.name like @del_column;
  45. set @ count = ( select count(*) from @tb);
  46. set @i = 1;
  47. while @i <= @count
  48. begin
  49. set @ sql = 'select @return=sum('+
  50. ( select column_name from @tb where rownum = @i) +
  51. ') from ' + @temp_name;
  52. exec sp_executesql @sql,N'@return int output',@return output;
  53. select @sql_delete_column =
  54. @sql_delete_column +
  55. case when @ return <> 0 then ' '
  56. else 'alter table '+@temp_name +
  57. ' drop column '+
  58. ( select column_name from @tb where rownum = @i) +
  59. ';'
  60. end
  61. set @i = @i + 1
  62. end
  63. --动态生成的删除列语句
  64. select @sql_delete_column
  65. /*
  66. (无列名)
  67. alter table ##temp drop column 优惠金额1;
  68. alter table ##temp drop column 优惠金额3;
  69. */
  70. --删除列
  71. exec(@sql_delete_column)
  72. --查询数据
  73. select * from ##temp;
  74. /*
  75. 住院号 床位 应收金额 优惠金额2 优惠金额4
  76. 00000 111 1000.000 0.000 10.000
  77. 00001 112 1000.000 0.000 0.000
  78. 00002 113 1000.000 0.000 0.000
  79. 00003 114 1000.000 0.000 20.000
  80. 00004 115 1000.000 2.000 3.000
  81. 00005 116 1000.000 0.000 0.000
  82. 00006 117 1000.000 0.000 0.000
  83. */


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


我的解法:

  
  
  1. drop table tb
  2. go
  3. create table tb(
  4. 型号 varchar( 20),年 int, 月 int, 日 int,
  5. 准确率 int, 缺到率 int,可用率 int
  6. )
  7. insert into tb
  8. select 'thd' , 2013, 1, 1 , 56 , 23 , 34
  9. union all select 'thd', 2013 , 1 , 1 , 66 , 77 , 54
  10. union all select 'thd', 2013 , 1 , 1 , 78 , 55 , 77
  11. union all select 'hhh', 2012 , 9 , 18 , 89 , 55 , 23
  12. union all select 'hhh', 2012 , 9 , 18 , 33 , 37 , 45
  13. union all select 'hhh', 2012 , 9 , 18 , 67 , 56 , 12
  14. go
  15. declare @ sql nvarchar( max);
  16. set @ sql = '';
  17. ;with t
  18. as
  19. (
  20. select *,
  21. ROW_NUMBER() over( partition by 型号,年,月,日 order by @@servername) as rownum
  22. from tb
  23. )
  24. select
  25. @ sql = @ sql + ',max(case when rownum = '+ cast( rownum as varchar)+ ' then 准确率 else null end) as 准确率' +
  26. ',max(case when rownum = '+ cast( rownum as varchar)+ ' then 缺到率 else null end) as 缺到率' +
  27. ',max(case when rownum = '+ cast( rownum as varchar)+ ' then 可用率 else null end) as 可用率'
  28. from t
  29. group by rownum
  30. select @ sql = 'select 型号,年,月,日' + @ sql +
  31. ' from (select *,
  32. ROW_NUMBER() over(partition by 型号,年,月,日 order by @@servername) as rownum
  33. from tb)t' +
  34. ' group by 型号,年,月,日'
  35. --select @sql
  36. exec(@ sql)
  37. /*
  38. 型号 年 月 日 准确率 缺到率 可用率 准确率 缺到率 可用率 准确率 缺到率 可用率
  39. hhh 2012 9 18 89 55 23 33 37 45 67 56 12
  40. thd 2013 1 1 56 23 34 66 77 54 78 55 77
  41. */

猜你喜欢

转载自www.cnblogs.com/lonelyxmas/p/12020004.html