Solved

SQL ignore dupe records

Posted on 2014-03-11
9
232 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
Back Up Your Microsoft Windows Server®

Back up all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

 
LVL 40

Expert Comment

by:lcohan
ID: 39921150
OR:

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

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
 

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
 

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

Secure Your Active Directory - April 20, 2017

Active Directory plays a critical role in your company’s IT infrastructure and keeping it secure in today’s hacker-infested world is a must.
Microsoft published 300+ pages of guidance, but who has the time, money, and resources to implement? Register now to find an easier way.

Question has a verified solution.

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

Suggested Solutions

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.

733 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