SQL查询返回重复记录

时间:2016-07-04 08:01:26

标签: sql sql-server

该查询返回两次每条记录。例如,'SpCall'字段是主键,查询返回:

result of the sql query

我错过了什么吗?

QUERY:

SELECT CONVERT(varchar(10),BusinessRevenue.Entrance_Date,103) as
       analistEnterance,Teams.DivisionName, Teams.DepartmentManager, 
       BusinessRevenue.Team_Name, BusinessRevenue.Account_Manager,
       BusinessRevenue.Forigen_Infra_Count,BusinessRevenue.Partner_Infra_Count,
       BusinessRevenue.Networks_Adsl_Count,BusinessRevenue.Pri_Or_Siptopri,BusinessRevenue.SIP,
       BusinessRevenue.Centrex,BusinessRevenue.Hosting , BusinessRevenue.Vps_Server,
       BusinessRevenue.Mabal,BusinessRevenue.Equipment_Income, BusinessRevenue.SpCall
FROM BusinessRevenue LEFT JOIN Teams ON BusinessRevenue.Team_Name=Teams.TeamName
WHERE
   BusinessRevenue.SpCall IS NOT NULL 
   AND BusinessRevenue.Entrance_Year =2016
   AND BusinessRevenue.Entrance_Date <='30/06/2016'
   AND BusinessRevenue.Entrance_Date >='01/06/2016'
   AND BusinessRevenue.Team_Name NOT IN('xxx','yyy') 

非常感谢你的时间和考虑!

2 个答案:

答案 0 :(得分:1)

你应该尝试使用&#34; Teams&#34;的主键进行左连接。表。 正如您所写,&#34; BusinessRevenue.SpCall&#34;是&#34; BusinessRevenue&#34;的主键。表,但正如你做左边的&#34; BusinessRevenue&#34;使用&#34; TeamName&#34;专栏&#34;团队&#34;表,可能超过一行&#34;团队&#34;与&#34; BusinessRevenue.Team_Name&#34;匹配,因此SpCall不再是结果表的主键。

答案 1 :(得分:0)

尝试使用SELECT DISTINCT,这将删除重复的行。