我现在又一个人员的基本信息名单,想在ms-server中通过查询基本信息表中的人员姓名来生成一个常用的
签到信息表,如何写这个语句?
基本表:
ID name age
1 张三 18
2 李四 20
想通过查询表中的name来生成下面的表
签到表
时间 张三 李四
2009年9月14日 正常 请假
2009年9月13日 病假 事假
签到信息表,如何写这个语句?
基本表:
ID name age
1 张三 18
2 李四 20
想通过查询表中的name来生成下面的表
签到表
时间 张三 李四
2009年9月14日 正常 请假
2009年9月13日 病假 事假
二、根据master..sptvalue生成连续值。
--行转列
if object_id('tempdb..#test1') is not null
begin
drop table #test1
end
create table #test1
(
t_id int primary key,
t_name varchar(10)
)
if object_id('tempdb..#test2') is not null
begin
drop table #test2
end
create table #test2
(
t_id int ,
t_mon varchar(10),
total int
)insert into #test1
select 1,'张三' union all
select 2,'李四' union all
select 3,'王五'insert into #test2
select 1,'1月',120 union all
select 2,'1月',256 union all
select 3,'1月',512 union all select 1,'2月',120 union all
select 2,'2月',256 union all
select 3,'2月',512select * from #test2declare @s varchar (500)
set @s =''
select @s = @s + ',sum(case t_mon when '''+ t_mon +''' then total else 0 end) as ''' + t_mon+'''' from (select Distinct t_mon from #test2) a order by t_mon
print(@s)
set @s = 'select #test1.t_id,#test1.t_name'+@s+' from #test2 inner join #test1 on #test1.t_id = #test2.t_id group by #test1.t_id,#test1.t_name order by #test1.t_id '
print(@s)
exec (@s)
t_id t_name 1月 2月
----------- ---------- ----------- -----------
1 张三 120 120
2 李四 256 256
3 王五 512 512
drop再create 的方式感觉不好,能不能用aleter?