--测试数据:
drop table tb1
create table tb1
(id char(10),ph char(10),sb char(10))insert into tb1 values('1','123','987')
insert into tb1 values('1','124','987')
insert into tb1 values('1','125','987')
insert into tb1 values('1','123','986')
insert into tb1 values('1','124','986')
insert into tb1 values('1','125','986')
insert into tb1 values('2','123','987')
insert into tb1 values('2','124','987')
insert into tb1 values('3','333','666')
insert into tb1 values('3','333','777')--要求显示的结果如下:
1 123 987
  124 986
  125
2 123 987
  124
3 333 666
      777

解决方案 »

  1.   

    补充一下 数据库为sql 2000
      

  2.   


    select id=case when a.rowid=1 then a.id else '' end),a.ph,b.sb
     from
    (select rowid=row_number()over(order by id,ph asc),* from (select distinct id,ph from tb)M)a
     inner join
    (select row_id=row_number()over(partition by id order by sb desc),* from (select distinct id,sb from tb) N)b
     on a.id=b.id
    --有待改进
      

  3.   


    row_number()是2005版本以后才有的 2000用不了
      

  4.   


    select 
    id = case when isnull(a.bh,b.bh) > 1 then '' else isnull(a.id,b.id) end,
    ph = isnull(a.ph,''),
    sb = isnull(b.sb,'')
    from (
    select a.id, a.ph, bh = (select count(distinct ph) from tb1 where id = a.id and ph <= a.ph)
    from tb1 a 
    group by a.id, a.ph
    ) a full join (
    select a.id, a.sb, bh = (select count(distinct sb) from tb1 where id = a.id and sb <= a.sb)
    from tb1 a
    group by a.id, a.sb
    ) b on a.id = b.id and a.bh = b.bh
    order by isnull(a.id,b.id),isnull(a.bh,b.bh)/*
    id         ph         sb
    ---------- ---------- ----------
    1          123        986       
               124        987       
               125                  
    2          123        987       
               124                  
    3          333        666       
                          777       (7 行受影响)
    */
    分太少了,记得结贴哦
      

  5.   

    我还以为结不了了呢 谢谢coleling