原表数据
工号                 日期                        刷卡时间
--------        ----------------------  -----------------------
00000001     2009-01-02 00:00:00.000  07:50:00.000
00000001        2009-01-02 00:00:00.000  12:01:00.000
00000001        2009-01-02 00:00:00.000  13:25:00.000
00000001        2009-01-02 00:00:00.000  17:32:00.000
00000001        2009-01-02 00:00:00.000  18:24:00.000
00000002     2009-01-02 00:00:00.000  07:50:00.000
00000002        2009-01-02 00:00:00.000  12:01:00.000
00000002        2009-01-02 00:00:00.000  13:25:00.000
00000002        2009-01-02 00:00:00.000  17:32:00.000
00000002        2009-01-02 00:00:00.000  18:24:00.000想得到如下结果。不知SQL 如何下
工号        日期                         时间1         时间2         时间3      时间4         时间5
------------------------------------------------------------------------------------------------
00000001  2009-01-02 00:00:00.000  07:50:00.000 12:01:00.000 13:25:00.000 17:32:00.000 18:24:00.000
00000002  2009-01-02 00:00:00.000  07:50:00.000 12:01:00.000 13:25:00.000 17:32:00.000 18:24:00.000谢谢!

解决方案 »

  1.   

    select 工号,日期,
      max(case px when 1 then 刷卡时间 else null end)  刷卡时间1,
      max(case px when 2 then 刷卡时间 else null end)  刷卡时间2,
      max(case px when 3 then 刷卡时间 else null end)  刷卡时间3,
      max(case px when 4 then 刷卡时间 else null end)  刷卡时间4,
      max(case px when 5 then 刷卡时间 else null end)  刷卡时间5
    from
    (
      select * , px = (select count(1) from tb where 工号 = t.工号 and 日期 = t.日期 and 刷卡时间 < t.刷卡时间) + 1 from tb
    ) m
    group by 工号,日期
      

  2.   

    --SQL SERVER 2000 静态SQL,指同人同天最多五次刷卡select 工号,日期,
      max(case px when 1 then 刷卡时间 else null end)  刷卡时间1,
      max(case px when 2 then 刷卡时间 else null end)  刷卡时间2,
      max(case px when 3 then 刷卡时间 else null end)  刷卡时间3,
      max(case px when 4 then 刷卡时间 else null end)  刷卡时间4,
      max(case px when 5 then 刷卡时间 else null end)  刷卡时间5
    from
    (
      select * , px = (select count(1) from tb where 工号 = t.工号 and 日期 = t.日期 and 刷卡时间 < t.刷卡时间) + 1 from tb
    ) m
    group by 工号,日期--SQL SERVER 2000 静态SQL,指同人同天刷卡次数不定.
    declare @sql varchar(8000)
    set @sql = 'select 工号,日期 '
    select @sql = @sql + ' , max(case px when ''' + cast(px as varchar) + ''' then 刷卡时间 else null end) [刷卡时间' + cast(px as varchar) + ']'
    from (select distinct px from (select * , px = (select count(1) from tb where 工号 = t.工号 and 日期 = t.日期 and 刷卡时间 < t.刷卡时间) + 1 from tb)m) as a
    set @sql = @sql + ' from (select * , px = (select count(1) from tb where 工号 = t.工号 and 日期 = t.日期 and 刷卡时间 < t.刷卡时间) + 1 from tb)m group by 工号,日期'
    exec(@sql) 
      

  3.   

    create table tb(工号 varchar(10),日期 datetime,刷卡时间 varchar(20)) 
    insert into tb values('00000001' , '2009-01-02 00:00:00.000', '07:50:00.000') 
    insert into tb values('00000001' , '2009-01-02 00:00:00.000', '12:01:00.000') 
    insert into tb values('00000001' , '2009-01-02 00:00:00.000', '13:25:00.000') 
    insert into tb values('00000001' , '2009-01-02 00:00:00.000', '17:32:00.000') 
    insert into tb values('00000001' , '2009-01-02 00:00:00.000', '18:24:00.000') 
    insert into tb values('00000002' , '2009-01-02 00:00:00.000', '07:50:00.000') 
    insert into tb values('00000002' , '2009-01-02 00:00:00.000', '12:01:00.000') 
    insert into tb values('00000002' , '2009-01-02 00:00:00.000', '13:25:00.000') 
    insert into tb values('00000002' , '2009-01-02 00:00:00.000', '17:32:00.000') 
    insert into tb values('00000002' , '2009-01-02 00:00:00.000', '18:24:00.000')
    go--SQL SERVER 2000 静态SQL,指同人同天最多五次刷卡select 工号,日期,
      max(case px when 1 then 刷卡时间 else null end)  刷卡时间1,
      max(case px when 2 then 刷卡时间 else null end)  刷卡时间2,
      max(case px when 3 then 刷卡时间 else null end)  刷卡时间3,
      max(case px when 4 then 刷卡时间 else null end)  刷卡时间4,
      max(case px when 5 then 刷卡时间 else null end)  刷卡时间5
    from
    (
      select * , px = (select count(1) from tb where 工号 = t.工号 and 日期 = t.日期 and 刷卡时间 < t.刷卡时间) + 1 from tb t
    ) m
    group by 工号,日期
    /*
    工号         日期                                                     刷卡时间1                刷卡时间2                刷卡时间3                刷卡时间4                刷卡时间5                
    ---------- ------------------------------------------------------ -------------------- -------------------- -------------------- -------------------- -------------------- 
    00000001   2009-01-02 00:00:00.000                                07:50:00.000         12:01:00.000         13:25:00.000         17:32:00.000         18:24:00.000
    00000002   2009-01-02 00:00:00.000                                07:50:00.000         12:01:00.000         13:25:00.000         17:32:00.000         18:24:00.000(所影响的行数为 2 行)
    */--SQL SERVER 2000 静态SQL,指同人同天刷卡次数不定.
    declare @sql varchar(8000)
    set @sql = 'select 工号,日期 '
    select @sql = @sql + ' , max(case px when ''' + cast(px as varchar) + ''' then 刷卡时间 else null end) [刷卡时间' + cast(px as varchar) + ']'
    from (select distinct px from (select * , px = (select count(1) from tb where 工号 = t.工号 and 日期 = t.日期 and 刷卡时间 < t.刷卡时间) + 1 from tb t)m) as a
    set @sql = @sql + ' from (select * , px = (select count(1) from tb where 工号 = t.工号 and 日期 = t.日期 and 刷卡时间 < t.刷卡时间) + 1 from tb t)m group by 工号,日期'
    exec(@sql) /*
    工号         日期                                                     刷卡时间1                刷卡时间2                刷卡时间3                刷卡时间4                刷卡时间5                
    ---------- ------------------------------------------------------ -------------------- -------------------- -------------------- -------------------- -------------------- 
    00000001   2009-01-02 00:00:00.000                                07:50:00.000         12:01:00.000         13:25:00.000         17:32:00.000         18:24:00.000
    00000002   2009-01-02 00:00:00.000                                07:50:00.000         12:01:00.000         13:25:00.000         17:32:00.000         18:24:00.000(所影响的行数为 2 行)
    */drop table tb 
      

  4.   

    提示:
    消息 4104,级别 16,状态 1,第 3 行
    无法绑定由多个部分组成的标识符 "t.工号"。
    消息 4104,级别 16,状态 1,第 3 行
    无法绑定由多个部分组成的标识符 "t.日期"。
    消息 4104,级别 16,状态 1,第 3 行
    无法绑定由多个部分组成的标识符 "t.刷卡时间"。没看得太懂