没看明白
A3    B3    C3        2   --生成的结果中C3,C6去那里了?
A5    B6    C6        2

解决方案 »

  1.   

    原表中Subtype=2 時 C3字段就不要.
      

  2.   

    还有,1,2,3之间是怎么对应的
    即,为什么A3与A1是同一行,而不是与A2同一行
      

  3.   

    1 , 2, 3  是類別的分類代號,比如 1 對就書藉, 2對應廣告   3 對應 紙板之類的.而下面的A1,A2,B1,B2......  只不過是一些記錄數據,沒什么關聯.
    我為了方便就把那些數據全部打成A,B,C來表示.
      

  4.   

    --查询(假设C1是主键,如果没有可以构成主键的字段,无法直接得到结果)
    select Cx=isnull(a.C1,''),Cy=isnull(a.C2,''),Cz=isnull(a.C3,'')
    ,Cw=isnull(b.C1,''),Cv=isnull(b.C2,'')
    ,Co=isnull(c.C1,''),Cp=isnull(c.C2,''),Cq=isnull(c.C3,'')
    from(
    select *,sid=(select sum(1) from tb where Subtype=1 and C1<=a.C1)
    from tb a
    where Subtype=1
    )a full join(
    select *,sid=(select sum(1) from tb where Subtype=2 and C1<=a.C1)
    from tb a
    where Subtype=2
    )b on a.sid=b.sid full join(
    select *,sid=(select sum(1) from tb where Subtype=2 and C1<=a.C1)
    from tb a
    where Subtype=3
    )c on a.sid=c.sid
      

  5.   

    --测试--测试数据
    create table tb(C1 varchar(10),C2 varchar(10),C3 varchar(10),Subtype int)
    insert tb select 'A1','B1','C1',1
    union all select 'A2','B2','C2',1
    union all select 'A3','B3','C3',2
    union all select 'A4','B4','C4',3
    union all select 'A5','B5','C5',1
    union all select 'A5','B6','C6',2
    go--查询(假设C1是主键,如果没有可以构成主键的字段,无法直接得到结果)
    select Cx=isnull(a.C1,''),Cy=isnull(a.C2,''),Cz=isnull(a.C3,'')
    ,Cw=isnull(b.C1,''),Cv=isnull(b.C2,'')
    ,Co=isnull(c.C1,''),Cp=isnull(c.C2,''),Cq=isnull(c.C3,'')
    from(
    select *,sid=(select sum(1) from tb where Subtype=1 and C1<=a.C1)
    from tb a
    where Subtype=1
    )a full join(
    select *,sid=(select sum(1) from tb where Subtype=2 and C1<=a.C1)
    from tb a
    where Subtype=2
    )b on a.sid=b.sid full join(
    select *,sid=(select sum(1) from tb where Subtype=2 and C1<=a.C1)
    from tb a
    where Subtype=3
    )c on a.sid=c.sid
    go--删除测试
    drop table tb/*--测试结果Cx   Cy   Cz   Cw   Cv   Co   Cp   Cq  
    ---- ---- ---- ---- ---- ---- ---- ----
    A1   B1   C1   A3   B3   A4   B4   C4
    A2   B2   C2   A5   B6 
    A5   B5   C5     (所影响的行数为 3 行)
    --*/
      

  6.   

    對,不過現在大家不必考慮第二行,第三行.....我現在就要下面這個樣子,只有一行就可以了,
    即每個類別都是 Select top 1 .....Cx    Cy     Cz    Cw      Cv     Co    Cp    Cq
    ---------------------------------------------------------
    A1    B1     C1    A3      B3     A4    B4    C4
    這個跟 Select Union All 有點類同
    只不過 Select Union All 是對行的操作
    我現在要對列進行操作
    急啊...
      

  7.   

    --每类只要一行
    select Cx=isnull(a.C1,''),Cy=isnull(a.C2,''),Cz=isnull(a.C3,'')
    ,Cw=isnull(b.C1,''),Cv=isnull(b.C2,'')
    ,Co=isnull(c.C1,''),Cp=isnull(c.C2,''),Cq=isnull(c.C3,'')
    from(
    select top 1 * from tb a where Subtype=1
    )a,(
    select top 1 * from tb a where Subtype=2
    )b,(
    select top 1 * from tb a where Subtype=3
    )c
      

  8.   

    我也贴过参考吧!
    create table tA 
    (
    C1 varchar(3),
    C2 varchar(3),
    C3 varchar(3),
    Subtype int
    )insert tA(C1 ,C2 ,C3,Subtype)
    select           'A1' ,   'B1' ,   'C1' ,       1
    union all select 'A2'  ,  'B2' ,   'C2'  ,      1
    union all select 'A3' ,   'B3',    'C3' ,       2
    union all select 'A5'  ,  'B5' ,   'C5' ,       1
    union all select 'A5' ,   'B6' ,   'C6'  ,      2
    union all select 'A4' ,   'B4' ,   'C4'  ,      3select identity(int,1,1) fid,C1 as Cx,C2 as Cy,C3 as Cz into #t1
    from tA where Subtype=1select identity(int,1,1) fid, C1 as Cw,C2 as Cv  into #t2
    from tA where Subtype=2select identity(int,1,1) fid, C1 as Co,C2 as Cp,C3 as Cq  into #t3
    from tA where Subtype=3select Cx, Cy,Cz ,isnull(Cw,'') Cw, isnull(Cv,'') Cv,
    isnull( Co,'')Co,isnull( Cp,0)Cp, isnull(Cq,0)Cq
    from #t1 t1 
    left join #t2 t2 on t1.fid=t2.fid
    left join #t3 t3 on t1.fid=t3.fid
      

  9.   

    Cx   Cy   Cz   Cw   Cv   Co   Cp   Cq   
    ---- ---- ---- ---- ---- ---- ---- ---- 
    A1   B1   C1   A3   B3   A4   B4   C4
    A2   B2   C2   A5   B6        0    0
    A5   B5   C5                  0    0(所影响的行数为 3 行)