在Sql中隐藏重复的行值

时间:2014-06-17 06:53:28

标签: sql sql-server sql-server-2005 sql-server-2008-r2 sql-server-2012

我有一张如下表格

 UserName |  Fee  | ReferenceName | Billed 
 ----------------------------------------
 John        200     John            Yes
 Khalid      100     Chintu          No
 Khalid      100     Naga            Yes
 Khalid      100     Sudheer         Yes
 Sathish     200     aaaa            No
 Sathish     200     bbbb            Yes
 Suresh      100     cccc            Yes

我想要像下面这样的外出:

 UserName |  Fee  | ReferenceName | Billed 
 ----------------------------------------
 John        200     John            Yes
 Khalid      100     Chintu          No
                     Naga            Yes
                     Sudheer         Yes
 Sathish     200     aaaa            No
                     bbbb            Yes
 Suresh      100     cccc            Yes

重复的名字不会第二次出现在这里' UserName'和'费用'列是重复的列..

2 个答案:

答案 0 :(得分:2)

请尝试:

select 
    case when RNum=1 then UserName else '' end UserName,
    case when RNum=1 then Fee else '' end Fee,
    ReferenceName,
    Billed 
from(
    select 
        *, 
        ROW_NUMBER() over (partition by UserName order by UserName) RNum 
    from tbl
)x

答案 1 :(得分:1)

可能你可以试试这个

select 
    case when RANKK=1 then USERNAME else '' end USERNAME,
    case when RANKK=1 then FEE else '' end FEE,
    REFERENCENAME,
    BILLED 
from(
    select 
        A.*, 
        RANK over (partition by USERNAME,FEE order by USERNAME) AS RANKK 
    from MY_TABLE A
)x