--> 测试数据: #T
if object_id('tempdb.dbo.#T') is not null drop table #T
create table #T (D1 varchar(5),D2 int,D3 varchar(1))
insert into #T
select '商品1',1,'L' union all
select '商品2',1,'L' union all
select '商品3',1,'L' union all
select '商品4',2,'B' union all
select '商品5',2,'B' union all
select '商品6',2,'B' union all
select '商品7',2,'B' union all
select '商品8',2,'B' union all
select '商品9',2,'B'
;
with T as
(
select id=row_number()over(partition by D2 order by D1),* from #T
)
select
isnull(a.D1,''), isnull(ltrim(a.D2),''), isnull(a.D3,''),
isnull(b.D1,''), isnull(ltrim(b.D2),''), isnull(b.D3,'')
from
(select * from T where D2=1) a
full join
(select * from T where D2=2) b
on a.id=b.id/*
商品1   1            L    商品4   2            B
商品2   1            L    商品5   2            B
商品3   1            L    商品6   2            B
                          商品7   2            B
                          商品8   2            B
                          商品9   2            B
*/

解决方案 »

  1.   

    create table #a (d1 varchar(5),d2 varchar(2),d3 varchar(2))insert into #a values('商品1','1','L')
    insert into #a values('商品2','1','L')
    insert into #a values('商品3','1','L')
    insert into #a values('商品4','2','B')
    insert into #a values('商品5','2','B')
    insert into #a values('商品6','2','B')
    insert into #a values('商品7','2','B')
    insert into #a values('商品8','2','B')
    insert into #a values('商品9','2','B')select isnull(c.d1,''),isnull(c.d2,''),isnull(c.d3,''),isnull(d.d1,''),isnull(d.d2,''),isnull(d.d3,'') from
    (SELECT id=(select count(1) from #a where d2=a.d2 and d1<a.d1)+1,a.* from #a a where d2=1) c
    full join
    (SELECT id=(select count(1) from #a where d2=b.d2 and d1<b.d1)+1,b.* from #a b where d2=2) d
    on c.id=d.id
    ----- ---- ---- ----- ---- ----
    商品1   1    L    商品4   2    B
    商品2   1    L    商品5   2    B
    商品3   1    L    商品6   2    B
                    商品7   2    B
                    商品8   2    B
                    商品9   2    B(6 行受影响)
      

  2.   

    不能用一条select 语句实现吗?
      

  3.   

    select isnull(c.d1,''),isnull(c.d2,''),isnull(c.d3,''),isnull(d.d1,''),isnull(d.d2,''),isnull(d.d3,'') from
    (SELECT id=(select count(1) from #a where d2=a.d2 and d1<a.d1)+1,a.* from #a a where d2=1) c
    full join
    (SELECT id=(select count(1) from #a where d2=b.d2 and d1<b.d1)+1,b.* from #a b where d2=2) d
    on c.id=d.id这不就是一句吗???
      

  4.   

    create table tb (D1 varchar(5),D2 int,D3 varchar(1))
    insert into tb
    select '商品1',1,'L' union all
    select '商品2',1,'L' union all
    select '商品3',1,'L' union all
    select '商品4',2,'B' union all
    select '商品5',2,'B' union all
    select '商品6',2,'B' union all
    select '商品7',2,'B' union all
    select '商品8',2,'B' union all
    select '商品9',2,'B'select isnull(m.D1,'') D1 , isnull(cast(m.D2 as varchar) , '') D2, isnull(m.D3 , '') D3 , n.D1 , n.D2 , n.D3 from
    (select * , px = (select count(1) from tb where d3 = 'L' and d1 < t.d1) + 1 from tb t where d3 = 'L') m
    right join
    (select * , px = (select count(1) from tb where d3 = 'B' and d1 < t.d1) + 1 from tb t where d3 = 'B') n
    on m.px = n.pxdrop table tb/*
    D1    D2                             D3   D1    D2          D3   
    ----- ------------------------------ ---- ----- ----------- ---- 
    商品1   1                              L    商品4   2           B
    商品2   1                              L    商品5   2           B
    商品3   1                              L    商品6   2           B
                                              商品7   2           B
                                              商品8   2           B
                                              商品9   2           B(所影响的行数为 6 行)
    */