Solved

join on multiple tables

Posted on 2009-07-06
4
660 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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

It seems a simple enough task, yet I see repeated questions asking how to do it: how to pass data between two forms. In this article, I will show you the different mechanisms available for you to do just that. This article is directed towards the .N…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

820 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