不是去整行,行都是唯一的,如,现有下表: 零件号      名称   装入上级   装入数量  总数量
TI4.005      A     AA          1       1
TI4.005     A     BB          1       1
TI4.005     A     CC          2       2
TI4.005                               4希望得到的结果:
TI4.005      A     AA          1       1
                   BB          1       1
                   CC          2       2
                                       4
如何用sql实现?重复值 字段

解决方案 »

  1.   


    with a1 as
    (
    select *,row_number() over (partition by 零件号 order by 装入上级) re
    from 表
    )
    select case when re=1 then 零件号 else '' end 零件号,*
    from a1
      

  2.   

    是这样的吗:;with t(零件号,      名称,   装入上级 ,  装入数量,  总数量)
    as
    (
    select 'TI4.005',    'A',     'AA',          1,       1 union all
    select 'TI4.005',    'A',     'BB',          1,       1 union all
    select 'TI4.005',    'A',     'CC',          2,       2 union all
    select 'TI4.005',    null,      null,        null,    4 
    ),tt
    as
    (
    select *,
           row_number() over(partition by 零件号 order by 零件号) as rownum
    from t
    )
    select case when 零件号 = (select top 1 零件号 from tt t2 
                               where t1.零件号 = t2.零件号
                                     and t2.rownum < t1.rownum 
                               order by t2.rownum desc)
                      then null
                else 零件号
           end as 零件号,
           
           case when 名称 = (select top 1 名称 from tt t2 
                               where t1.零件号 = t2.零件号
                                     and t2.rownum < t1.rownum 
                               order by t2.rownum desc)
                      then null
                else 名称
           end as 名称,
           
           case when 装入上级 = (select top 1 名称 from tt t2 
                               where t1.零件号 = t2.零件号
                                     and t2.rownum < t1.rownum 
                               order by t2.rownum desc)
                      then null
                else 装入上级
           end as 装入上级,
           
           装入数量,
           总数量 
    from tt t1
    /*
    零件号 名称     装入上级 装入数量 总数量
    TI4.005 A     AA     1     1
    NULL NULL BB     1     1
    NULL NULL CC     2     2
    NULL NULL NULL NULL 4
    */
      

  3.   


    create table lvb
    (零件号 varchar(10),名称 varchar(5),装入上级 varchar(5),装入数量 int,总数量 int)insert into lvb
     select 'TI4.005','A','AA',1,1 union all
     select 'TI4.005','A','BB',1,1 union all
     select 'TI4.005','A','CC',2,2 union all
     select 'TI4.005','','',4,null
    with t as
    (select 零件号,名称,装入上级,装入数量,总数量,
            row_number() over(partition by 零件号 order by getdate()) 'rn'
     from lvb
    )
    select case when exists(select 1 from t b 
                            where b.零件号=a.零件号 and b.rn<a.rn and b.零件号<>'')
                then '' else a.零件号 end '零件号',
           case when exists(select 1 from t b 
                            where b.零件号=a.零件号 and b.rn<a.rn and b.名称=a.名称)
                then '' else a.名称 end '名称',
           case when exists(select 1 from t b 
                            where b.零件号=a.零件号 and b.rn<a.rn and b.装入上级=a.装入上级)
                then '' else a.装入上级 end '装入上级',
           a.装入数量,
           a.总数量
     from t a
     order by a.rn/*
    零件号       名称   装入上级  装入数量    总数量
    ---------- ----- ----- ----------- -----------
    TI4.005    A      AA      1           1
                      BB      1           1
                      CC      2           2
                              4           NULL(4 row(s) affected)
    */