select empid,case  when empdate = + empdate + then empdate +'' + emptime
            FROM (SELECT empdate,emptime  FROM  a)

解决方案 »

  1.   

    select empid,empno,
    min(case when (select count(*) from a where empid=x.empid and empno=x.empno and empdate=empdate and emptime<=x.emptime)=1 then empdate+' '+emptime end) as t1,
    min(case when (select count(*) from a where empid=x.empid and empno=x.empno and empdate=empdate and emptime<=x.emptime)=2 then empdate+' '+emptime end) as t2,
    min(case when (select count(*) from a where empid=x.empid and empno=x.empno and empdate=empdate and emptime<=x.emptime)=3 then empdate+' '+emptime end) as t3,
    min(case when (select count(*) from a where empid=x.empid and empno=x.empno and empdate=empdate and emptime<=x.emptime)=4 then empdate+' '+emptime end) as t4
    from a x
    group by empid,empno,empdate
    order by empid,empno,empdate
      

  2.   

    可還是不行﹐出現下列錯誤﹐請您幫我看看好嗎﹖謝謝您﹗﹗﹗﹗﹗
    伺服器: 訊息 130,層級 15,狀態 1,行 1
    Cannot perform an aggregate function on an expression containing an aggregate or a subquery.
    伺服器: 訊息 170,層級 15,狀態 1,行 3
    Line 3: Incorrect syntax near '='.
    伺服器: 訊息 170,層級 15,狀態 1,行 4
    Line 4: Incorrect syntax near '='.
    伺服器: 訊息 170,層級 15,狀態 1,行 5
    Line 5: Incorrect syntax near '='.
      

  3.   

    select *,row 1 into c from a
    declare @int int,@a int,@b varchar(20),@c datetime
    update c set @int=case when @a=empid and @b=empno and @c=empdate then @int+1 else 1 end,@a=empid,@b=empno,@c=empdate,row=@intdeclare @sql varchar(8000)
    set @sql='select empid,empno'
    select @sql=@sql+',case row when ''' + cast(row as varchar(10)) + ''' then convert(varchar(11),empdate,120)+emptime else null end as C'+ cast(row as varchar(10))
    from (select distinct row from c) as ccselect @sql=@sql+' from c group by empid,empno,convert(varchar(11),empdate,120)+emptime,empdate,emptime,row'
    exec (@sql)
      

  4.   

    select *,row 1 into c from a
    declare @int int,@a int,@b varchar(20),@c datetime
    update c set @int=case when @a=empid and @b=empno and @c=empdate then @int+1 else 1 end,@a=empid,@b=empno,@c=empdate,row=@intdeclare @sql varchar(8000)
    set @sql='select empid,empno'
    select @sql=@sql+',case row when ''' + cast(row as varchar(10)) + ''' then convert(varchar(11),empdate,120)+emptime else null end as t'+ cast(row as varchar(10))
    from (select distinct row from c) as ccselect @sql=@sql+' from c group by empid,empno,convert(varchar(11),empdate,120)+emptime,empdate,emptime,row'
    exec (@sql)
      

  5.   

    請問﹐你的ROW 1 表示什么呀﹖多謝了﹗﹗﹗
      

  6.   

    我没有测试,不知道对否?row为别名,1为初始值
      

  7.   

    能不能用下面的方式呢.这样就简单多了.empid    empno         所有日期                  1756 AA000042   2002-05-19 60328, 2002-05-19 60332,  2002-05-19 60336, 2002-05-19 60340
    ....
      

  8.   

    其实应该为 col 1
    表示所属的列在新加列中的位置 1表示在第一列,2表示在第2列....
      

  9.   

    就是,如果是蚂蚁这种情况的话可以
    create function getstr(@empid int,@empno varchar(20),@empdate datetime)
    returns varchar(2000)
    as 
    begin
    declare @str varchar(2000)
    set @str=''
    select @str=@str+','+convert(varchar(11),empdate,120)+emptime from a where empid=@empid and empno=@empno and empdate=@empdate
    set @str=right(@str,len(@str)-1)
    return @str
    end
    go
    select empid,empno,dbo.getstr(empid,empno,empdate) areas  from a group by empid,empno,empdate
      

  10.   

    ---测试数据create table #a(
    empid int,
    empno varchar(20),
    empdate varchar(20),
    emptime varchar(20)
    )
    goinsert #a
    select
    1756, 'AA000042', '2002-05-19',        '60328'
    union all
    select
    1756, 'AA000042', '2002-05-19',    '60332'
    union all
    select
    1756, 'AA000042', '2002-05-19',    '60336'
    union all
    select
    1756, 'AA000042', '2002-05-19',        '60340'
    union all
    select
    1756, 'AA000042', '2002-05-20',    '29755'
    union all
    select
    1756, 'AA000042', '2002-06-11',    '41864'
    union all
    select
    1756, 'AA000042', '2002-06-11',    '50900'
    union all
    select
    1757, 'AA000021', '2002-05-20',        '30200'
    union all
    select
    1759, 'AA000010', '2002-12-14',    '59400'
    union all
    select
    1759, 'AA000010', '2002-12-28',        '79740'
    union all
    select
    1759, 'AA000010', '2003-04-03',        '63360'
    union all
    select
    1760, 'AA000011', '2002-05-22',    '45005'
    union all
    select
    1760, 'AA000011', '2002-05-22',    '48408'
    union all
    select
    1760, 'AA000011', '2002-05-22',    '63005'
    union all
    select
    1760, 'AA000011', '2002-05-22',      '66441'
    go
    --语句
    select empid,empno,
    min(t1) as t1,
    min(t2) as t2,
    min(t3) as t3,
    min(t4) as t4
    from (
    select empid,empno,empdate,
    case when (select count(*) from #a where empid=x.empid and empno=x.empno and empdate=x.empdate and emptime<=x.emptime)=1 then empdate+' '+emptime end as t1,
    case when (select count(*) from #a where empid=x.empid and empno=x.empno and empdate=x.empdate and emptime<=x.emptime)=2 then empdate+' '+emptime end as t2,
    case when (select count(*) from #a where empid=x.empid and empno=x.empno and empdate=x.empdate and emptime<=x.emptime)=3 then empdate+' '+emptime end as t3,
    case when (select count(*) from #a where empid=x.empid and empno=x.empno and empdate=x.empdate and emptime<=x.emptime)=4 then empdate+' '+emptime end as t4
    from #a x
    ) as y
    group by empid,empno,empdate
    order by empid,empno,empdate
    go结果:
    empid       empno                t1                                        t2                                        t3                                        t4                                        
    ----------- -------------------- ----------------------------------------- ----------------------------------------- ----------------------------------------- ----------------------------------------- 
    1756        AA000042             2002-05-19 60328                          2002-05-19 60332                          2002-05-19 60336                          2002-05-19 60340
    1756        AA000042             2002-05-20 29755                          NULL                                      NULL                                      NULL
    1756        AA000042             2002-06-11 41864                          2002-06-11 50900                          NULL                                      NULL
    1757        AA000021             2002-05-20 30200                          NULL                                      NULL                                      NULL
    1759        AA000010             2002-12-14 59400                          NULL                                      NULL                                      NULL
    1759        AA000010             2002-12-28 79740                          NULL                                      NULL                                      NULL
    1759        AA000010             2003-04-03 63360                          NULL                                      NULL                                      NULL
    1760        AA000011             2002-05-22 45005                          2002-05-22 48408                          2002-05-22 63005                          2002-05-22 66441(所影响的行数为 8 行)