Solved

join on multiple tables

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

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 62

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 62

Expert Comment

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

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

The object model of .Net can be overwhelming at times – so overwhelming that quite trivial tasks often take hours of research. In this case, the task at hand was to populate the datagrid from SQL Server database in Visual Studio 2008 Windows applica…
This article describes relatively difficult and non-obvious issues that are likely to arise when creating COM class in Visual Studio and deploying it by professional MSI-authoring tools. It is assumed that the reader is already familiar with the cla…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

743 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now