如何把以下格式考勤表转换成每人二行,上行是上班时间,下行是下班时间,一个月的,且重复打卡时间只选一
号 码 姓名 时间
210002 27 张三 2007-08-15 10:30:59.000
210002 27 张三 2007-08-15 10:34:59.000
210002 27 张三 2007-08-16 07:47:59.000
210002 27 张三 2007-08-16 17:32:59.000
210002 27 张三 2007-08-17 07:50:59.000
210003 30 李四 2007-08-15 10:33:59.000
210003 30 李四 2007-08-15 10:34:59.000
210003 30 李四 2007-08-16 07:49:59.000
210003 30 李四 2007-08-16 17:32:59.000
210003 30 李四 2007-08-17 07:47:59.000类似以下格式
号 码 姓名 日期
210002 27 张三 10:30:59
17:32:59
210003 30 李四 10:33:59(舍10:34:59)
18:35:59
希望高手给予帮助

解决方案 »

  1.   

    1: 列轉為行:
    eg1:
    Create table test (name char(10),km char(10),cj int)
    go
    insert test values('張三','語文',80)
    insert test values('張三','數學',86)
    insert test values('張三','英語',75)
    insert test values('李四','語文',78)
    insert test values('李四','數學',85)
    insert test values('李四','英語',78)想變成姓名   語文   數學   英語
    張三   80     86     75
    李四   78     85     78
    declare @sql varchar(8000)
    set @sql = 'select name'
    select @sql = @sql + ',sum(case km when '''+km+''' then cj end) ['+km+']'
     from (select distinct km from test) as a
    select @sql = @sql+' from test group by name'
    exec(@sql)drop table test
      

  2.   

    create table r(c_id int,ma int,name1 varchar(10),c_date datetime)
    insert into r 
    select 210002,27,'张三','2007-08-15 10:30:59.000'
    union all
    select 210002,27,'张三','2007-08-15 10:34:59.000'
    union all
    select 210002,27,'张三','2007-08-16 07:47:59.000'
    union all
    select 210002,27,'张三','2007-08-16 17:32:59.000'
    union all
    select 210002,27,'张三','2007-08-17 07:50:59.000'
     union all
    select 210003,27,'李四','2007-08-15 10:33:59.000'
     union all
    select 210003,27,'李四','2007-08-15 10:34:59.000'
     union all
    select 210003,27,'李四','2007-08-16 07:49:59.000'
     union all
    select 210003,27,'李四','2007-08-16 17:32:59.000'
     union all
    select 210003,27,'李四','2007-08-17 07:47:59.000'
    -----------------------------
    select c_id,ma,name1,min(c_date)as c_date from r a where substring(convert(varchar(30),a.c_date,120),12,8) <'12:00:00' group by c_id,ma,name1
    union
    select c_id,ma,name1,max(c_date)as c_date from r a where substring(convert(varchar(30),a.c_date,120),12,8) >='12:00:00' 
    and substring(convert(varchar(30),a.c_date,120),12,8) < '24:00:00' group by c_id,ma,name1