SQL 多行相同值,只显示第一行的数据
显示效果:1   10
1   20
1   33
1   50
2   20需要显示的效果是:1    10
     20
     33
     50
2    20

解决方案 »

  1.   

    做个计数列,然后case when查询。
      

  2.   


    with cte as
    (
    select row_number() over (partition by id order by id) n ,id,num from tb
    )select id,num from cte where n = 1
    union  all
    select '',num from cte where n<> 1
      

  3.   


    --假设第一列为id,第二列为num
    ;with cte as(
    select rid=row_number() over (partition by id order by num),* from tb
    )
    select case when rid=1 then ltrim(id) else '' end,num from cte
      

  4.   


    create table tb
    (id int ,value int)
    insert into tb 
    select 1,10 union all
    select 1,20 union all
    select 1,33 union all
    select 1,50 union all
    select 2,20with cte as
    (
      select *,rowNum=ROW_NUMBER() over(partition by id order by value) from tb
    )
    select case when rowNum=1 then cast(id as varchar) else '' end AS id,value from cteid                             value
    ------------------------------ -----------
    1                              10
                                   20
                                   33
                                   50
    2                              20(5 行受影响)
      

  5.   

    select
       case px when 1 then ltrim(id) else '' end as id,
       value
    from  
       (select px=row_number()over(partition by id order by getdate()),* from tb) t
      

  6.   

    select case when col2 = (select min(col2) from tb where col1 = t.col1) then cast(col1 as varchar) else '' end col1,col2 from tb t
      

  7.   

    create table tb
    (id int ,value int)
    insert into tb 
    select 1,10 union all
    select 1,20 union all
    select 1,33 union all
    select 1,50 union all
    select 2,20
    ;
    with cte as
    (
      select *,rowNum=ROW_NUMBER() over(partition by id order by value) from tb
    )
    select case when rowNum=1 then cast(id as varchar) else '' end AS id,value from cte
    记得别把那个‘;’输起。