Jonathan Applebaum
Jonathan Applebaum

Reputation: 5986

SQL query returns duplicate records

That query returns each record twice. for example, the 'SpCall' field is primary key, and the query returns:

result of the sql query

am i missing something?

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') 

Thank you very much for your time and consideration!

Upvotes: 0

Views: 216

Answers (2)

Sergio
Sergio

Reputation: 503

Try using SELECT DISTINCT, this will remove duplicate rows.

Upvotes: 0

ddb
ddb

Reputation: 2435

You should try to do a left join using a primary key of "Teams" table. As you wrote, "BusinessRevenue.SpCall" is a primary key for "BusinessRevenue" table, but as you do a left join of "BusinessRevenue" with the "TeamName" column "Team" table, maybe more than one row in "Teams" have a match with "BusinessRevenue.Team_Name", so the SpCall is not anymore a primary key of the resulting table.

Upvotes: 1

Related Questions