原文:在论坛中出现的比较难的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. */

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

  1. 在论坛中出现的比较难的sql问题:42(动态行转列 考勤时间动态列)

    原文:在论坛中出现的比较难的sql问题:42(动态行转列 考勤时间动态列) 所以,觉得有必要记录下来,这样以后再次碰到这类问题,也能从中获取解答的思路.

  2. 在论坛中出现的比较难的sql问题:39(动态行转列 动态日期列问题)

    原文:在论坛中出现的比较难的sql问题:39(动态行转列 动态日期列问题) 最近,在论坛中,遇到了不少比较难的sql问题,虽然自己都能解决,但发现过几天后,就记不起来了,也忘记解决的方法了. 所以,觉 ...

  3. 在论坛中出现的比较难的sql问题:37(动态行转列 某一行数据转为列名)

    原文:在论坛中出现的比较难的sql问题:37(动态行转列 某一行数据转为列名) 所以,觉得有必要记录下来,这样以后再次碰到这类问题,也能从中获取解答的思路.

  4. 在论坛中出现的比较难的sql问题:36(动态行转列 解析json格式字符串)

    原文:在论坛中出现的比较难的sql问题:36(动态行转列 解析json格式字符串) 所以,觉得有必要记录下来,这样以后再次碰到这类问题,也能从中获取解答的思路.

  5. 在论坛中出现的比较难的sql问题:26(动态行专列+合并字符串、补足行数)

    原文:在论坛中出现的比较难的sql问题:26(动态行专列+合并字符串.补足行数) 最近,在论坛中,遇到了不少比较难的sql问题,虽然自己都能解决,但发现过几天后,就记不起来了,也忘记解决的方法了. 所 ...

  6. 在论坛中出现的比较难的sql问题:6(动态行转列 考试科目、排名动态列问题)

    原文:在论坛中出现的比较难的sql问题:6(动态行转列 考试科目.排名动态列问题) 所以,觉得有必要记录下来,这样以后再次碰到这类问题,也能从中获取解答的思路. 下面的几个问题,都是动态行转列的问题. ...

  7. 在论坛中出现的比较难的sql问题:46(日期条件出现的奇怪问题)

    原文:在论坛中出现的比较难的sql问题:46(日期条件出现的奇怪问题) 最近,在论坛中,遇到了不少比较难的sql问题,虽然自己都能解决,但发现过几天后,就记不起来了,也忘记解决的方法了. 所以,觉得有 ...

  8. 在论坛中出现的比较难的sql问题:45(用户在线登陆时间的小时、分钟计算问题)

    原文:在论坛中出现的比较难的sql问题:45(用户在线登陆时间的小时.分钟计算问题) 最近,在论坛中,遇到了不少比较难的sql问题,虽然自己都能解决,但发现过几天后,就记不起来了,也忘记解决的方法了. ...

  9. 在论坛中出现的比较难的sql问题:44(触发器专题 明细表插入数据时调用主表对应的数据)

    原文:在论坛中出现的比较难的sql问题:44(触发器专题 明细表插入数据时调用主表对应的数据) 最近,在论坛中,遇到了不少比较难的sql问题,虽然自己都能解决,但发现过几天后,就记不起来了,也忘记解决 ...

随机推荐

  1. peomethues 参数设置 监控网站 /usr/local/prometheus-2.13.0.linux-amd64/prometheus --config.file=/usr/local/prometheus-2.13.0.linux-amd64/prometheus.yml --web.listen-address=:9999 --web.enable-lifecycle

    probe_http_status_code{instance="xxxx",job="web_status"} probe_http_status_code{ ...

  2. Xamarin图表开发基础教程(2)OxyPlot框架

    Xamarin图表开发基础教程(2)OxyPlot框架 OxyPlot图表设计 OxyPlot是一个基于.Net的跨平台图表库.该图表库也支持Xamarin应用开发.该组件支持多种类型的图表.本章将主 ...

  3. 【转载】 180623 Conda install 本地压缩包文件tar.bz2

    原文地址“ https://blog.csdn.net/qq_33039859/article/details/80785535 ----------------------------------- ...

  4. openpyxl代码案例

    import datetimefrom random import choicefrom time import timefrom openpyxl import load_workbookfrom ...

  5. ABAP函数篇1 日期函数

    1. 日期格式字段检查 data:l_date type ekko-bedat. l_date = '20080901'. CALL FUNCTION 'DATE_CHECK_PLAUSIBILITY ...

  6. c#写windows服务 小demo

    前段时间做一个数据迁移项目,刚开始用B/S架构做的项目,但B/S要寄存在IIs中,而IIs又不稳定因素,如果重启IIs就要打开页面才能运行项目.有不便之处,就改用Windows服务实现.这篇就总结下, ...

  7. 【Leetcode_easy】884. Uncommon Words from Two Sentences

    problem 884. Uncommon Words from Two Sentences 题意:只要在两个句子中单词出现总次数大于1次即可. 注意掌握istringstream/map/set的使 ...

  8. tcp内存占用/socket内存占用

    net.ipv4.tcp_mem 内核分配给TCP连接的内存,单位是Page,1 Page = 4096 Bytes,可用命令查看: #getconf PAGESIZE 4096 net.ipv4.t ...

  9. SQL Server数据同步到Oracle

    一.分别配置SQL Server和oracle数据库的连接信息并测试连接. 二.新增数据同步任务,配置规则,运行任务,查看日志. 支持数据自动定时抽取,转换,汇聚同步.支持一对多,多对一,多对多等数据 ...

  10. Centos7.0操作系统加固常见方法

    1. 账号和口令 1.1 禁用或删除无用账号 减少系统无用账号,降低安全风险. 操作步骤 使用命令 userdel <用户名> 删除不必要的账号. 使用命令 passwd -l <用 ...