declare @temp table (group int, name varchar(10),[order] int)insert into @temp
select 1,'A',1 union all
select 1,'B',3 union all
select 1,'C',5 union all
select 2,'D',3 union all
select 2,'E',5 union all
select 3,'F',2 union all
select 3,'G',5  我希望得到的结果是1,A,1
1,B,3
1,C,5
2,A,1
2,D,3
2,E,5
3,A,1
3,F,2
3,B,3
3,G,5只要求得到这个结论就够了,要求一个比较高效的查询得出来
也就是这样根据以第一组结果为参照进行替换,后面的每组如果自定义覆盖了,就用自定义覆盖的如组2的D覆盖了本来的B,如果插入了就补齐,如组3增加了ORDER为2的一个设置项F,覆盖了order为3,5的原第一组设置项,因此应该是下面形式的列形式表现:
 1  A, NULL, B, C
 2  A, NULL, D, E
 3  A, F   , B, G

解决方案 »

  1.   

    查询出来的结论得按group分组,然后按order排序
      

  2.   

    那可能是我说的复杂了,就是这样我希望得到的结果是1,A,1
    1,B,3
    1,C,5
    2,A,1
    2,D,3
    2,E,5
    3,A,1
    3,F,2
    3,B,3
    3,G,5
    其实就是
    1,A,1
    1,B,3
    1,C,51,A,1
    2,D,3
    2,E,51,A,1
    3,F,2
    1,B,3
    3,G,5
      

  3.   

    下面是group为2时本来的结果,由于相同的group为一个组,因此在结果里需要把下面的group 统一为21,A,1
    2,D,3
    2,E,5变成2,A,1
    2,D,3
    2,E,5
      

  4.   

    是在不好修改group 也无所谓了啦,只要能够区分出那些是一个组的就OK
      

  5.   

    declare @temp table ([group] int, name varchar(10),[order] int)insert into @temp
    select 1,'A',1 union all
    select 1,'B',3 union all
    select 1,'C',5 union all
    select 2,'D',3 union all
    select 2,'E',5 union all
    select 3,'F',2 union all
    select 3,'G',5 ;with cte
    as
    (
    select a.[group],b.name ,b.[order]
    from @temp a,@temp b
    where a.[group] >1 and b.[group] = 1
    group by a.[group],b.name,b.[order])
    select * from cte t
    where not exists(select 1 from @temp where [group] = t.[group] and [order] = t.[order])
    union all
    select * from @temp
    order by 1,2
    group       name       order
    ----------- ---------- -----------
    1           A          1
    1           B          3
    1           C          5
    2           A          1
    2           D          3
    2           E          5
    3           A          1
    3           B          3
    3           F          2
    3           G          5(10 行受影响)
      

  6.   

    谢谢大家,特别感谢happyflystone我在你的基础上优化以后查询子树还可以小25%