消除SQL中的重复数据

时间:2014-01-10 11:06:57

标签: sql sql-server tsql sql-server-2012

我正在使用SQL Server 2012,我有一个名为Employee的表,我试图获得像

这样的结果
SrNo  | Name_of_Employee   |    Known_Technology    |   No_of_Times   
------+--------------------+------------------------+-----------------
    1 |  Mahesh Kumar      |        C#.net          |       2
      |                    |        SQL Server      |       3
      |                    |        ASP.NET         |       1
    2 |  John Dias         |        JAVA            |       2
      |                    |        VB.NET          |       1
      |                    |        Jquery          |       2
    3 |  Sijo Paul         |        Style Sheet     |       1
      |                    |        MySQL           |       2
    4 |  Nichol Sami       |        ASP.NET         |       3
      |                    |        JAVA            |       2
      |                    |        VB.NET          |       1

但我得到这样的

SrNo  | Name_of_Employee   |    Known_Technology    |   No_of_Times   
------+--------------------+------------------------+-----------------
    1 |  Mahesh Kumar      |        C#.net          |       2
    1 |  Mahesh Kumar      |        SQL Server      |       3
    1 |  Mahesh Kumar      |        ASP.NET         |       1
    2 |  John Dias         |        JAVA            |       2
    2 |  John Dias         |        VB.NET          |       1
    2 |  John Dias         |        Jquery          |       2
    3 |  Sijo Paul         |        Style Sheet     |       1
    3 |  Sijo Paul         |        MySQL           |       2
    4 |  Nichol Sami       |        ASP.NET         |       3
    4 |  Nichol Sami       |        JAVA            |       2
    4 |  Nichol Sami       |        VB.NET          |       1

如何获得所需的结果..

4 个答案:

答案 0 :(得分:6)

这就是分组和加入工作的方式。不要尝试使用SQL查询格式化数据以便向最终用户显示 - 这就是您的用户界面的用途。 SQL查询返回数据 - 由您的UI代码决定是否为您的用户设置格式。

答案 1 :(得分:2)

我完全赞同@Tim Rogers,我所给予的是一种解决方法。

declare @a as table (SrNo int, Name_of_Employee varchar(50), Known_Technology varchar(50), No_of_Times int)
insert into @a values 
(1, 'Mahesh Kumar','C#.net',2),
(1, 'Mahesh Kumar','SQL Server',3),
(1, 'Mahesh Kumar','ASP.NET',1),
(2, 'John Dias','JAVA',2),
(2, 'John Dias','VB.NET',1),
(2, 'John Dias','Jquery',2),
(3, 'Sijo Paul','Style Sheet',1),
(3, 'Sijo Paul','MySQL',2),
(4, 'Nichol Sami','ASP.NET',3),
(4, 'Nichol Sami','JAVA',2),
(4, 'Nichol Sami','VB.NET',1)

;with cte as(
select row_number() over (partition by Name_of_Employee order by SrNo) as id1,
SrNo, Name_of_Employee,Known_Technology,No_of_Times from @a )

select Name = case id1 when 1 then Name_of_Employee else '' end, 
Known_Technology, No_of_Times from cte

答案 2 :(得分:1)

即使我认为这是不好的做法:

with prep as (
   select srno  
        , name_of_employee
        , known_technology
        , no_of_times
        , row_number() over (partition by srno, name_of_emplyee order by known_technology, no_of_times) as c
    from your_table
)
select case when c = 1 then srno else null end as srno
     , case when c = 1 then name_of_employee else null end name_of_employee
     , known_technology
     , no_of_times
from prep

答案 3 :(得分:0)

我不是100%确定你所处的位置(Tim Rogers对于不使用SQL进行格式化是正确的),但似乎你确实需要两个表来确保数据的完整性:

员工,其中包含有关员工自己的信息 列:

  • SrNo
  • Name_Of_Employee

Know_Technologies,其中包含有关每位员工的已知技术的信息 列:

  • Employee_SrNo(Employee.SrNo的外键)
  • Technology_Name No_Of_Times

您将在Employee和Known_Technology表之间建立1-n关系。