Solved

join on multiple tables

Posted on 2009-07-06
4
659 Views
Last Modified: 2013-11-11
I have the following two tables in a SQL 2005 database.

people
--------
person_id (PK)
first_name
mid_name
last_name
empl_id

multiple_sources
----------
source
person_id (This is a FK to people.person_id)


Here is what I want to accomplish;
Write a script (using C# and LINQ) to identify all the records in the people table having the same empl_id - basically a "group by on empl_id having count(empl_id) > 1". The output needs to contain all the columns in the people table. Now, I also need to (left) join the people table with the multiple_sources table and exclude all the person_id's that have multiple_sources.source of either a "D" or a "Z".

I want to schedule to script to run once a week and forward the output to a group of people in my organization for data cleanup.

Can someone please help me get started?
Thank you.
0
Comment
Question by:IUAATech
  • 3
4 Comments
 
LVL 63

Accepted Solution

by:
Fernando Soto earned 500 total points
ID: 24790860
Hi IUAATech;

The following query will locate all people with the same empl_id and that the group is greater then 1.

Fernando
DataClasses1DataContext db = new DataClasses1DataContext();
 
var results = from p in db.peoples
              group p by p.empl_id into emplGroup
              where emplGroup.Count() > 1
              from g in emplGroup
              select g;
 
foreach (var empl in results)
{
    Console.WriteLine(empl.person_id + ", " + empl.first_name +
        empl.mid_name + ", " + empl.last_name + ", " + empl.empl_id);
}

Open in new window

0
 
LVL 63

Expert Comment

by:Fernando Soto
ID: 24818331
Hi IUAATech;

Are you still have issues with this?

Fernando
0
 

Author Comment

by:IUAATech
ID: 24853128
Thanks.
0
 
LVL 63

Expert Comment

by:Fernando Soto
ID: 24853171
Not a problem, glad to help.  ;=)
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Suggested Solutions

Flash (http://en.wikipedia.org/wiki/Adobe_Flash) has evolved over the years to where it has become a masterful tool for displaying content screen.  It has excellent layout placement, UI precision as well as rendering capabilities. This, along with t…
A basic question.. “What is the Garbage Collector?” The usual answer given back: “Garbage collector is a background thread run by the CLR for freeing up the memory space used by the objects which are no longer used by the program.” I wondered …
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …

831 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