请问一条SQL语句的写法

yunshiyu 2005-04-03 03:40:37
有表如:
name num date things
王健 1 6-15 事假
王峰 1 6-6 迟到
王力奇 1 5-28 迟到
范围 1 5-30 迟到
陈雪梅 1 6-21 迟到
李国鹏 1 5-28 早退
王峰 1 6-4 早退
葛洪岩 1 6-24 旷工
葛洪岩 1 6-25 旷工
张晓丽 1 5-26 旷工
。。。
。。。
需要得到表:
姓名: 事假次数 事假日期 迟到次数 迟到日期 早退次数 早退日期 旷工次数 旷工日期
王健 1 6-15
王峰 1 6-6
葛洪岩 2 6-24/6-25
。。。。
其中 things中的项目名不固定,也就是说要生成的表的列名和列数也不固定
请教各位高人,谁有好办法
...全文
196 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
yunshiyu 2005-04-05
  • 打赏
  • 举报
回复
做个临时表,把需要得到的数据一项项更新进去是可以做到的
但我想一定有比这更好的办法吧?
wqhj2000 2005-04-04
  • 打赏
  • 举报
回复
还有个方法,不用游标而用递归SELECT完成。

declare @txt_sql varchar(8000)
declare @txt_things varchar(8000)

set @txt_sql=''
set @txt_things=''

select
@txt_things=@txt_things+'sum (case things when '''+ things +''' then num else 0 end) as ' +things+'次数,'
from test
group by things


set @txt_sql='select name,'+substring(@txt_things,1,len(@txt_things)-1)
set @txt_sql=@txt_sql +' from test group by name order by name'
print @txt_sql
exec sp_sqlexec @txt_sql
go
wqhj2000 2005-04-04
  • 打赏
  • 举报
回复
create table test(name varchar(20),num int,date varchar(20),things varchar(20))
go
insert test
values('王健', 1, '6-15', '事假')
insert test
values('王峰', 1, '6-6', '迟到')
insert test
values('王力奇', 1, '5-28', '迟到')
insert test
values('范围', 1, '5-30', '迟到')
insert test
values('陈雪梅', 1, '6-21', '迟到')
insert test
values('李国鹏', 1, '5-28', '早退')
insert test
values('王峰', 1, '6-4', '早退')
insert test
values('葛洪岩', 1, '6-24', '旷工')
insert test
values('张晓丽', 1, '5-26', '旷工')
go



declare @txt_sql varchar(8000)
declare @txt_date varchar(8000)
declare @txt_things varchar(8000)

set @txt_sql=''
set @txt_date=''
set @txt_things=''

declare c_things cursor
for
select distinct things from test

open c_things
fetch c_things into @txt_things

while @@fetch_status=0
begin
set @txt_sql=@txt_sql+'sum (case things when ''' +@txt_things+''' then num else 0 end) as ' +@txt_things+'次数,'
fetch c_things into @txt_things
end
close c_things
deallocate c_things

set @txt_sql='select name,'+substring(@txt_sql,1,len(@txt_sql)-1)
set @txt_sql=@txt_sql +' from test group by name order by name'
print @txt_sql
exec sp_sqlexec @txt_sql
go

drop table test
go

结果:
==========================================
name 迟到次数 旷工次数 事假次数 早退次数
陈雪梅 1 0 0 0
范围 1 0 0 0
葛洪岩 0 1 0 0
李国鹏 0 0 0 1
王峰 1 0 0 1
王健 0 0 1 0
王力奇 1 0 0 0
张晓丽 0 1 0 0
=========================================
yunshiyu 2005-04-04
  • 打赏
  • 举报
回复
ip
wqhj2000 2005-04-04
  • 打赏
  • 举报
回复
TO: yunshiyu(陨石雨)
如果要达到“次数大于1的项目后面的日期要加到一起”的目的,在一个批处理里实现可能有点困难,如楼上所说,也许用临时表不失为一个省力的方法。
ziping 2005-04-04
  • 打赏
  • 举报
回复
建个临时表算了
把要的数据导进出
yunshiyu 2005-04-04
  • 打赏
  • 举报
回复
wqhj2000(阿杰) :你得到的结果不是我想要的,不但有次数,还要有日期啊,次数大于1的项目后面的日期要加到一起的:4-4/4-5
云中客 2005-04-03
  • 打赏
  • 举报
回复
select '事假次数'=
case things when '事假' then num
else ''
end,
'事假日期'=
case things when '事假' then date
else ''
end,
.........

以此类推
doubleming 2005-04-03
  • 打赏
  • 举报
回复
请问如何得分啊???
wqhj2000 2005-04-03
  • 打赏
  • 举报
回复
我遇到过类似问题,是用游标实现动态SQL的方法。明天到公司把代码帖上来。
yunshiyu 2005-04-03
  • 打赏
  • 举报
回复
wqhj2000(阿杰) :不用试,一定不对
写是一定能写出来的,就是想找个比较好的方法,效率尽量高的算法
cmlcsdn 2005-04-03
  • 打赏
  • 举报
回复
首先用游标存储表列things,然后一一取出即可。
wqhj2000 2005-04-03
  • 打赏
  • 举报
回复
declare @txt_sql varchar(8000)
declare @txt_date varchar(8000)
declare @txt_things varchar(8000)

set @txt_sql=''
set @txt_date=''

declare cursor c_things
for
select distinct things from table

open c_things
fetch next c_things into @txt_things

while @@state=0
begin
txt_sql=txt_sql+'sum (case things when''' +@txt_things+'''then num) as 事假次数,'

fetch next c_things into @txt_things
end
close c_things

set txt_sql='select name,'+txt_sql
set txt_sql=txt_sql +'from table group by name'

=========================
以上是思路,未测试。

34,576

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧