在论坛中出现的比较难的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
型号 年 月 日 准确率 缺到率 可用率
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*/
- 在论坛中出现的比较难的sql问题:15(生成动态删除列语句 分组内多行转为多列)
- 在论坛中出现的比较难的sql问题:37(动态行转列 某一行数据转为列名)
- 在论坛中出现的比较难的sql问题:6(动态行转列 考试科目、排名动态列问题)
- 在论坛中出现的比较难的sql问题:39(动态行转列 动态日期列问题)
- 在论坛中出现的比较难的sql问题:42(动态行转列 考勤时间动态列)
- 在论坛中出现的比较难的sql问题:8(递归问题 树形结构分组)
- 在论坛中出现的比较难的sql问题:3(row_number函数 分组查询)
- 在论坛中出现的比较难的sql问题:4(row_number函数+子查询 分组连续编号问题)
- 在论坛中出现的比较难的sql问题:24(生成时间段)
- 在论坛中出现的比较难的sql问题:10(删除多表中的同一个外键)
- 在论坛中出现的比较难的sql问题:19(row_number函数 行转列、sql语句记流水)
- 在论坛中出现的比较难的sql问题:26(动态行专列+合并字符串、补足行数)
- 在论坛中出现的比较难的sql问题:36(动态行转列 解析json格式字符串)
- 在论坛中出现的比较难的sql问题:12(递归问题2 拆分字符串)
- 在论坛中出现的比较难的sql问题:23(随机填充问题)
- 在论坛中出现的比较难的sql问题:30(row_number函数 物料组合问题)
- 在论坛中出现的比较难的sql问题:33(递归 连续日期问题 )
- 在论坛中出现的比较难的sql问题:34(递归 获取连续值问题)
- editplus 删除空行
- Android相机开发2
- 用Vivado/SDK实现microblaze的一些总结
- mysql字段类型简介
- 单例模式
- 在论坛中出现的比较难的sql问题:15(生成动态删除列语句 分组内多行转为多列)
- 自定义 CRichEditCtrl 控件
- mac 上好用的文本编辑器
- 使用ClosedXML开发Office之--Excel的开发
- C#多线程与异步的区别
- eclipse 函数编译时报错:undefined reference to `pthread_create’
- 【分享】Daily and Sports Activities Dataset Data Set(日常和体育活动数据集)
- Android 下使用tcpdump网络抓包方法
- linux(debian) 下安装navicat premium 10 中文XX版.