ordNo   node    isAction        isActionDate   subnode  userName
2000 1   1 2009-09-21 0 tanxiaofen
2000 2   null  null     0 yhqing
2000 2   null  null     0 yangming
2000 3   null  null     0 thomas
2000 4   null null     0 kp1230 1   null  null     0 tanxiaofen
1230 2   null  null     0 yhqing
1230 2   null  null     0 yangming
1230 3   null  null     0 thomas
1230 4  null  null     0 kp如果isAction 和 isActionDate不为空,则查询出来比它的node大1的值的userName的值做为一个新的字段newuserName的值
如果isAction 和 isActionDate为空,则把node的最小值的userName的值作为newuserName的值
即我输入一个用户名可以得到他的当前下一个节点的用户
结果如下:ordNo   node    isAction        isActionDate   subnode  userName    nextUserName
2000 1   1 2009-09-21 0 tanxiaofen  yhqing
1230 1   null  null     0 tanxiaofen tanxiaofen

解决方案 »

  1.   

    DECLARE @TB TABLE([ordNo] INT, [node] INT, [isAction] INT, [isActionDate] DATETIME, [subnode] INT, [userName] VARCHAR(10))
    INSERT @TB 
    SELECT 2000, 1, 1, '2009-09-21', 0, 'tanxiaofen' UNION ALL 
    SELECT 2000, 2, null, null, 0, 'yhqing' UNION ALL 
    SELECT 2000, 2, null, null, 0, 'yangming' UNION ALL 
    SELECT 2000, 3, null, null, 0, 'thomas' UNION ALL 
    SELECT 2000, 4, null, null, 0, 'kp' UNION ALL 
    SELECT 1230, 1, null, null, 0, 'tanxiaofen' UNION ALL 
    SELECT 1230, 2, null, null, 0, 'yhqing' UNION ALL 
    SELECT 1230, 2, null, null, 0, 'yangming' UNION ALL 
    SELECT 1230, 3, null, null, 0, 'thomas' UNION ALL 
    SELECT 1230, 4, null, null, 0, 'kp'SELECT *,(SELECT TOP 1 userName FROM @TB WHERE ordNo=T.ordNo AND (isAction IS NULL OR (isAction IS NOT NULL AND node>1)) ORDER BY node)
    FROM @TB AS T
    WHERE node=1
    /*
    ordNo       node        isAction    isActionDate                                           subnode     userName              
    ----------- ----------- ----------- ------------------------------------------------------ ----------- ---------- ---------- 
    2000        1           1           2009-09-21 00:00:00.000                                0           tanxiaofen yangming
    1230        1           NULL        NULL                                                   0           tanxiaofen tanxiaofen
    */
      

  2.   

    create TABLE tb([ordNo] INT, [node] INT, [isAction] INT, [isActionDate] DATETIME, [subnode] INT, [userName] VARCHAR(10))
    INSERT tb 
    SELECT 2000, 1, 1, '2009-09-21', 0, 'tanxiaofen' UNION ALL 
    SELECT 2000, 2, null, null, 0, 'yhqing' UNION ALL 
    SELECT 2000, 2, null, null, 0, 'yangming' UNION ALL 
    SELECT 2000, 3, null, null, 0, 'thomas' UNION ALL 
    SELECT 2000, 4, null, null, 0, 'kp' UNION ALL 
    SELECT 1230, 1, null, null, 0, 'tanxiaofen' UNION ALL 
    SELECT 1230, 2, null, null, 0, 'yhqing' UNION ALL 
    SELECT 1230, 2, null, null, 0, 'yangming' UNION ALL 
    SELECT 1230, 3, null, null, 0, 'thomas' UNION ALL 
    SELECT 1230, 4, null, null, 0, 'kp'select * , 
           case when isAction is not null and isActionDate is not null then
                     (select top 1 username from tb where ordno = t.ordno and node = t.node + 1) 
                else
                     (select top 1 username from tb where ordno = t.ordno order by node)
           end newuserName
    from tb t drop table tb/*
    ordNo       node        isAction    isActionDate                                           subnode     userName   newuserName 
    ----------- ----------- ----------- ------------------------------------------------------ ----------- ---------- ----------- 
    2000        1           1           2009-09-21 00:00:00.000                                0           tanxiaofen yhqing
    2000        2           NULL        NULL                                                   0           yhqing     tanxiaofen
    2000        2           NULL        NULL                                                   0           yangming   tanxiaofen
    2000        3           NULL        NULL                                                   0           thomas     tanxiaofen
    2000        4           NULL        NULL                                                   0           kp         tanxiaofen
    1230        1           NULL        NULL                                                   0           tanxiaofen tanxiaofen
    1230        2           NULL        NULL                                                   0           yhqing     tanxiaofen
    1230        2           NULL        NULL                                                   0           yangming   tanxiaofen
    1230        3           NULL        NULL                                                   0           thomas     tanxiaofen
    1230        4           NULL        NULL                                                   0           kp         tanxiaofen(所影响的行数为 10 行)*/
      

  3.   

    再帮我改下那个查询出来的结果:
    ordNo  node  isAction  isActionDate subnode userName    nextUserName      status 
    2000    1    1         2009-09-21   0       tanxiaofen  yhqing,yangming  正在处理
    1230    1    null      null         0       tanxiaofen  tanxiaofen        待处理
    因为以下3条记录第一条已处理 第2和第3是同一个级别的,所以要把他们的nextUser用逗号连接起来
    那个状态是根据isAction、isActionDate 来做判断的如果都不为空 则显示为已完成,如果都为空则表示待处理,如果有一条记录则表示处理中  
    2000 1  1 2009-09-21 0 tanxiaofen 
    2000 2  null null    0 yhqing 
    2000 2  null null    0 yangming 
      

  4.   

    /*
    标题:按某字段合并字符串之一(简单合并)
    作者:爱新觉罗.毓华(十八年风雨,守得冰山雪莲花开)
    时间:2008-11-06
    地点:广东深圳描述:将如下形式的数据按id字段合并value字段。
    id    value
    ----- ------
    1     aa
    1     bb
    2     aaa
    2     bbb
    2     ccc
    需要得到结果:
    id     value
    ------ -----------
    1      aa,bb
    2      aaa,bbb,ccc
    即:group by id, 求 value 的和(字符串相加)
    */
    --1、sql2000中只能用自定义的函数解决
    create table tb(id int, value varchar(10))
    insert into tb values(1, 'aa')
    insert into tb values(1, 'bb')
    insert into tb values(2, 'aaa')
    insert into tb values(2, 'bbb')
    insert into tb values(2, 'ccc')
    gocreate function dbo.f_str(@id int) returns varchar(100)
    as
    begin
        declare @str varchar(1000)
        set @str = ''
        select @str = @str + ',' + cast(value as varchar) from tb where id = @id
        set @str = right(@str , len(@str) - 1)
        return @str
    end
    go--调用函数
    select id , value = dbo.f_str(id) from tb group by iddrop function dbo.f_str
    drop table tb
    --2、sql2005中的方法
    create table tb(id int, value varchar(10))
    insert into tb values(1, 'aa')
    insert into tb values(1, 'bb')
    insert into tb values(2, 'aaa')
    insert into tb values(2, 'bbb')
    insert into tb values(2, 'ccc')
    goselect id, [value] = stuff((select ',' + [value] from tb t where id = tb.id for xml path('')) , 1 , 1 , '')
    from tb
    group by iddrop table tb
    --3、使用游标合并数据
    create table tb(id int, value varchar(10))
    insert into tb values(1, 'aa')
    insert into tb values(1, 'bb')
    insert into tb values(2, 'aaa')
    insert into tb values(2, 'bbb')
    insert into tb values(2, 'ccc')
    go
    declare @t table(id int,value varchar(100))--定义结果集表变量
    --定义游标并进行合并处理
    declare my_cursor cursor local for
    select id , value from tb
    declare @id_old int , @id int , @value varchar(10) , @s varchar(100)
    open my_cursor
    fetch my_cursor into @id , @value
    select @id_old = @id , @s=''
    while @@FETCH_STATUS = 0
    begin
        if @id = @id_old
           select @s = @s + ',' + cast(@value as varchar)
        else
          begin
            insert @t values(@id_old , stuff(@s,1,1,''))
            select @s = ',' + cast(@value as varchar) , @id_old = @id
          end
        fetch my_cursor into @id , @value
    END
    insert @t values(@id_old , stuff(@s,1,1,''))
    close my_cursor
    deallocate my_cursorselect * from @t
    drop table tb
      

  5.   

    CREATE TABLE test2(
    ordNo INT, 
    node INT,
    isAction INT,
    isActionDate DATETIME,
    subnode INT,
    userName VARCHAR(20)
    );INSERT INTO test2(ordNo, node, isAction, isActionDate, subnode, userName)
    SELECT 2000,1,1,'2009-09-21',0,'luoyoumou' UNION ALL
    SELECT 2000,2,NULL,NULL,0,'yhqing' UNION ALL
    SELECT 2000,2,NULL,NULL,0,'yangming' UNION ALL
    SELECT 2000,3,NULL,NULL,0,'thomas' UNION ALL
    SELECT 2000,4,NULL,NULL,0,'kp' UNION ALL
    SELECT 1230,1,NULL,NULL,0,'tanxiaofen' UNION ALL
    SELECT 1230,2,NULL,NULL,0,'yhqing' UNION ALL
    SELECT 1230,2,NULL,NULL,0,'yangming' UNION ALL
    SELECT 1230,3,NULL,NULL,0,'thomas' UNION ALL
    SELECT 1230,4,NULL,NULL,0,'kp';--楼主:你要的查询语句如下:
    SELECT t1.ordNo t1_ordNo, t2.ordNo t2_ordNo,
           t1.node t1_node, t2.node t2_node, 
           t1.isAction, t1.isActionDate, t1.subnode, t1.userName,
           t2.userName AS newuserName
      FROM test2 t1 JOIN test2 t2
        ON t1.ordNo=t2.ordNo AND t1.node=t2.node-1
     WHERE (t1.isAction IS NOT NULL AND t1.isActionDate IS NOT NULL)
    UNION ALL
    SELECT t1.ordNo t1_ordNo, t2.ordNo t2_ordNo,
           t1.node t1_node, t2.node t2_node, 
           t1.isAction, t1.isActionDate, t1.subnode, t1.userName,
           t2.userName AS newuserName
      FROM test2 t1 JOIN test2 t2
        ON t1.ordNo=t2.ordNo AND t2.node=(SELECT MIN(node) FROM test2 t3 WHERE t3.ordNo=t2.ordNo)
      WHERE (t1.isAction IS NULL AND t1.isActionDate IS NULL)
      

  6.   

    DROP TABLE test2;CREATE TABLE test2(
    ordNo INT, 
    node INT,
    isAction INT,
    isActionDate DATETIME,
    subnode INT,
    userName VARCHAR(20)
    );INSERT INTO test2(ordNo, node, isAction, isActionDate, subnode, userName)
    SELECT 2000,1,1,'2009-09-21',0,'luoyoumou' UNION ALL
    SELECT 2000,2,NULL,NULL,0,'yhqing' UNION ALL
    SELECT 2000,2,NULL,NULL,0,'yangming' UNION ALL
    SELECT 2000,3,NULL,NULL,0,'thomas' UNION ALL
    SELECT 2000,4,NULL,NULL,0,'kp' UNION ALL
    SELECT 1230,1,NULL,NULL,0,'tanxiaofen' UNION ALL
    SELECT 1230,2,NULL,NULL,0,'yhqing' UNION ALL
    SELECT 1230,2,NULL,NULL,0,'yangming' UNION ALL
    SELECT 1230,3,NULL,NULL,0,'thomas' UNION ALL
    SELECT 1230,4,NULL,NULL,0,'kp';--楼主:你要的查询语句如下:(去掉你不需要的字段)
    SELECT t1.ordNo, t1.node, t1.isAction, t1.isActionDate, 
           t1.subnode, t1.userName, t2.userName AS newuserName
      FROM test2 t1 JOIN test2 t2
        ON t1.ordNo=t2.ordNo AND t1.node=t2.node-1
     WHERE (t1.isAction IS NOT NULL AND t1.isActionDate IS NOT NULL)
    UNION ALL
    SELECT t1.ordNo, t1.node, t1.isAction, t1.isActionDate, 
           t1.subnode, t1.userName, t2.userName AS newuserName
      FROM test2 t1 JOIN test2 t2
        ON t1.ordNo=t2.ordNo AND t2.node=(SELECT MIN(node) FROM test2 t3 WHERE t3.ordNo=t2.ordNo)
      WHERE (t1.isAction IS NULL AND t1.isActionDate IS NULL)
      

  7.   

    --楼主:要按ordNo分组,对吧?
      

  8.   

    SQL CODEcreate view v_tb as 
    select   *,case when isAction is not null and isActionDate is not null then 
    (select top 1   userName from tb b where a.ordNo=b.ordNo and a.node<>b.node order by  node ) 
    else   
     (select top 1 username from tb c where a.ordno =c.ordno order by node)  
     
          end  nextUserName  ,
    case when isAction is not null and isActionDate is not null 
          then '正在处理'
     else  '待处理'
          end  status  
           from tb a
     
    select * from v_tb a 
    where not exists (select 1 from v_tb b where a.ordno=b.ordno and a.node>b.node )搞定
    楼主的数据像是电信数据,不知道对不对