数据库的数据如下STAMPDAY  EmployeeCD stamptype   stamptime 
2007-02-28 2200399    1          08:02:00.000
2007-02-28 2200399    2          18:01:00.000
2007-03-01 2200399    1          08:08:00.000(3 件処理されました)
这里stamptype   为1的是出勤 2的时候是退勤我想得到数据如下
STAMPDAY  EmployeeCD 出勤    退勤 
2007-02-28 2200399   08:02   18:01
2007-03-01 2200399   08:08  null
我做了一个,能够实现,但是显示两条同样的数据,如下
STAMPDAY  EmployeeCD 出勤    退勤 
2007-02-28 2200399   08:02   18:01
2007-02-28 2200399   08:02   18:01
2007-03-01 2200399   08:08  null经过处理可以实现一个,但是个人认为方法太笨,再次向各位高手求教其他方法

解决方案 »

  1.   


    Select 
    STAMPDAY,
    EmployeeCD,
    Max(Case stamptype When 1 Then stamptime Else Null End) As 出勤,
    Max(Case stamptype When 2 Then stamptime Else Null End) As 退勤
    From TableName
    Group By STAMPDAY,EmployeeCD
      

  2.   

    Create Table TEST
    (STAMPDAY Varchar(10),
     EmployeeCD Char(7),
     stamptype Int,
     stamptime  Varchar(20))
    Insert TEST Select '2007-02-28', '2200399',    1,          '08:02:00.000'
    Union All Select '2007-02-28', '2200399',    2,          '18:01:00.000'
    Union All Select '2007-03-01', '2200399',    1,          '08:08:00.000'
    GO
    Select 
    STAMPDAY,
    EmployeeCD,
    Max(Case stamptype When 1 Then stamptime Else Null End) As 出勤,
    Max(Case stamptype When 2 Then stamptime Else Null End) As 退勤
    From TEST
    Group By STAMPDAY,EmployeeCD
    GO
    Drop Table TEST
    --Result
    /*
    STAMPDAY EmployeeCD 出勤 退勤
    2007-02-28 2200399 08:02:00.000 18:01:00.000
    2007-03-01 2200399 08:08:00.000 NULL
    */
      

  3.   

    楼上实在是高
    第一种可以实现
    但是不知道max做起的作用 如果没有max然后就回出来两条
    这个max的范围是什么?一EmployeeCD为范围的一个集合?
      

  4.   

    Select 
    STAMPDAY,
    EmployeeCD,
    Max(Case stamptype When 1 Then CONVERT(varchar(8),stamptime) End) As 出勤,
    Max(Case stamptype When 2 Then CONVERT(varchar(8),stamptime) End) As 退勤
    From TEST
    Group By STAMPDAY,EmployeeCD
    --不用写else
      

  5.   

    declare @t table
    (
    STAMPDAY varchar(10),EmployeeCD int,stamptype int,stamptime varchar(12)
    )
    insert @t select '2007-02-28', 2200399, 1  ,'08:02:00.000'
    union all select '2007-02-28', 2200399, 2 ,'18:01:00.000'
    union all select '2007-03-01', 2200399 , 1, '08:08:00.000'select t1.stampday,t1.employeecd,出勤=left(t1.stamptime,5),退勤=left(t2.stamptime,5) from
    (select * from @t where stamptype=1) t1
    full join
    (select * from @t where stamptype=2) t2
    on t1.stampday=t2.stampday
      

  6.   

    yudi010(★★芳★★) ( ) 信誉:100    Blog  2007-03-01 10:41:29  得分: 0  
     
     
       楼上实在是高
    第一种可以实现
    但是不知道max做起的作用 如果没有max然后就回出来两条
    这个max的范围是什么?一EmployeeCD为范围的一个集合?
      
     
    ----------我這裡的Max和後面的Group By接合起來用的。你的就是沒有按照STAMPDAY,EmployeeCD分組統計,所以才會出現3條數據的結果。
      

  7.   


          select 'STAMPDAY'= A.STAMPDAY,
                A.EmployeeCD as EmployeeCD,
                '出勤'= A.stamptime,
                '退勤'= B.STAMPTIME
           from stamp as A left join stamp as B on A.STAMPTYPE!=B.STAMPTYPE and A.STAMPDAY=B.STAMPDAY and A.EmployeeCD=B.EmployeeCD
           WHERE A.Stamptype=1
      

  8.   

    楼上实在是高
    第一种可以实现
    但是不知道max做起的作用 如果没有max然后就回出来两条
    这个max的范围是什么?一EmployeeCD为范围的一个集合?
    max其实没作用,你仔细看,case其实把数据选出来了。
    但是 where后面有gruop by ,所以你选的内容要加上max,sum等这些函数
      

  9.   

    恩 对头 
    gruop by ---要加上max,sum等这些函数  配合起来用就好了