Solved

SQL ignore dupe records

Posted on 2014-03-11
9
233 Views
Last Modified: 2014-03-17
Hi,

I'm writing a query that will return people based on the office they exist in. The data below is a made sample.
I need the result set of a query for everyone in office 'nyc', except for 'mike smith' because he is already in the office 'la'. I only want people returned for a given office if they don't already exist in another office.

So based on the data below, for the result set I would like
john doe and john lennon.

It seems like it should be simple but I'm having a hard time.
Thanks!
Nacht


Table: employees

fname: mike
lname: smith
office: nyc

fname: mike
lname: smith
office: la

fname: john
lname: doe
office: nyc

fname: john
lname: lennon
office: nyc
0
Comment
Question by:nachtmsk
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
  • 2
  • +1
9 Comments
 
LVL 10

Expert Comment

by:aboo_s
ID: 39921130
SELECT fname,lname FROM employees WHERE office = 'nyc' AND COUNT(SELECT * FROM employees WHERE fname = fname AND lname=lname AND office = 'la') = 0 ;
0
 
LVL 40

Expert Comment

by:lcohan
ID: 39921140
select fname,lname,office from employees
group by office
having COUNT(distinct fname) = 1
0
 
LVL 10

Expert Comment

by:aboo_s
ID: 39921142
or SELECT fname as fname1,lname as lname1 FROM employees WHERE office = 'nyc' AND COUNT(SELECT * FROM employees WHERE fname = fname1 AND lname=lname1 AND office = 'la') = 0 ;
0
Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

 
LVL 40

Expert Comment

by:lcohan
ID: 39921150
OR:

select fname,lname,office from employees
group by office
having COUNT(fname+lname) = 1
0
 
LVL 1

Author Comment

by:nachtmsk
ID: 39921199
Icohan,
What your suggesting seems exactly what I need.
However, when I'm running it I'm getting a syntax error. I'm using SQL Server 2005

Msg 156, Level 15, State 1, Line 7
Incorrect syntax near the keyword 'SELECT'.
Msg 102, Level 15, State 1, Line 7
Incorrect syntax near ')'.


Ideas?
Nacht
0
 
LVL 1

Author Comment

by:nachtmsk
ID: 39921222
I tried this one: select fname,lname,office from employeeList
group by office
having COUNT(fname+lname) = 1

and got this error message
Msg 8120, Level 16, State 1, Line 1
Column 'employeeList.fname' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.
0
 
LVL 40

Expert Comment

by:lcohan
ID: 39921255
Sorry I added those to the count and miss them from group by...this should work:

select fname,lname,office from employeeList
group by office,fname,lname
having COUNT(fname+lname) = 1

For "proof" you could add the "count" column and try the reverse like

select fname,lname,office,COUNT(fname+lname) as numb from employeeList
group by office,fname,lname
having COUNT(fname+lname) > 1
0
 
LVL 1

Author Comment

by:nachtmsk
ID: 39921302
Thanks.
that worked, but it returns too much. I don't want EITHER record returned if there is a dupe of it. The query you gave me returns once instance of the duped record. I want everyone except all instances of the dupe record. Possible?
0
 
LVL 41

Accepted Solution

by:
Sharath earned 500 total points
ID: 39921399
try this query
select fname,lname
  from employees
 group by fname,lname
having count(distinct office) = 1
  and max(office) = 'nyc'
  and min(office) = 'nyc'

Open in new window

http://sqlfiddle.com/#!3/4216a/1
0

Featured Post

Simplifying Server Workload Migrations

This use case outlines the migration challenges that organizations face and how the Acronis AnyData Engine supports physical-to-physical (P2P), physical-to-virtual (P2V), virtual to physical (V2P), and cross-virtual (V2V) migration scenarios to address these challenges.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
In part one, we reviewed the prerequisites required for installing SQL Server vNext. In this part we will explore how to install Microsoft's SQL Server on Ubuntu 16.04.
Via a live example, show how to shrink a transaction log file down to a reasonable size.
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

628 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question