Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Help with Joins.....


Help with Joins.....

Author
Message
ccmret
ccmret
Grasshopper
Grasshopper (23 reputation)Grasshopper (23 reputation)Grasshopper (23 reputation)Grasshopper (23 reputation)Grasshopper (23 reputation)Grasshopper (23 reputation)Grasshopper (23 reputation)Grasshopper (23 reputation)

Group: General Forum Members
Points: 23 Visits: 94
I am trying to get a EmpID where the PayID is not null. My problem is that an employee may have several EmpID’s. So I am currently getting all ID’s for an employee. I only want the EmpID for ones that have a PayID. Any ideas?

SELECT Distinct a.*,
b.Station,
c.EmpID
FROM #NoStaffID as a
Left JOIN #Station b
On a.City = b.City
JOIN Employee.Payroll c
On a.City=c.City
WHERE c.PayID is not null
Sean Lange
Sean Lange
SSCoach
SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)

Group: General Forum Members
Points: 16562 Visits: 17010
In order to help we will need a few things:

1. Sample DDL in the form of CREATE TABLE statements
2. Sample data in the form of INSERT INTO statements
3. Expected results based on the sample data

Please take a few minutes and read the first article in my signature for best practices when posting questions.

_______________________________________________________________

Need help? Help us help you.

Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

Need to split a string? Try Jeff Moden's splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
dwain.c
dwain.c
SSCarpal Tunnel
SSCarpal Tunnel (4.2K reputation)SSCarpal Tunnel (4.2K reputation)SSCarpal Tunnel (4.2K reputation)SSCarpal Tunnel (4.2K reputation)SSCarpal Tunnel (4.2K reputation)SSCarpal Tunnel (4.2K reputation)SSCarpal Tunnel (4.2K reputation)SSCarpal Tunnel (4.2K reputation)

Group: General Forum Members
Points: 4249 Visits: 6431
ccmret (7/9/2013)
I am trying to get a EmpID where the PayID is not null. My problem is that an employee may have several EmpID’s. So I am currently getting all ID’s for an employee. I only want the EmpID for ones that have a PayID. Any ideas?

SELECT Distinct a.*,
b.Station,
c.EmpID
FROM #NoStaffID as a
Left JOIN #Station b
On a.City = b.City
JOIN Employee.Payroll c
On a.City=c.City
WHERE c.PayID is not null


Shouldn't at least one of your JOINs be ON empID?


My mantra: No loops! No CURSORs! No RBAR! Hoo-uh!

My thought question: Have you ever been told that your query runs too fast?

My advice:
INDEXing a poor-performing query is like putting sugar on cat food. Yeah, it probably tastes better but are you sure you want to eat it?
The path of least resistance can be a slippery slope. Take care that fixing your fixes of fixes doesn't snowball and end up costing you more than fixing the root cause would have in the first place.


Need to UNPIVOT? Why not CROSS APPLY VALUES instead?
Since random numbers are too important to be left to chance, let's generate some!
Learn to understand recursive CTEs by example.
Splitting strings based on patterns can be fast!
My temporal SQL musings: Calendar Tables, an Easter SQL, Time Slots and Self-maintaining, Contiguous Effective Dates in Temporal Tables
Jeff Moden
Jeff Moden
SSC-Forever
SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)

Group: General Forum Members
Points: 45053 Visits: 39901
ccmret (7/9/2013)
I am trying to get a EmpID where the PayID is not null. My problem is that an employee may have several EmpID’s.


Gosh... unless you have a DateActivated and DateInactivated column, having multiple EmpIDs for the same employee is a fundamental problem. Are you sure there's no activation/deactivation dates associated with the EmpIDs?

--Jeff Moden

RBAR is pronounced ree-bar and is a Modenism for Row-By-Agonizing-Row.
First step towards the paradigm shift of writing Set Based code:
Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column.
Although they tell us that they want it real bad, our primary goal is to ensure that we dont actually give it to them that way.
Although change is inevitable, change for the better is not.
Just because you can do something in PowerShell, doesnt mean you should. Wink

Helpful Links:
How to post code problems
How to post performance problems
Forum FAQs
Eugene Elutin
Eugene Elutin
Hall of Fame
Hall of Fame (3K reputation)Hall of Fame (3K reputation)Hall of Fame (3K reputation)Hall of Fame (3K reputation)Hall of Fame (3K reputation)Hall of Fame (3K reputation)Hall of Fame (3K reputation)Hall of Fame (3K reputation)

Group: General Forum Members
Points: 3042 Visits: 5478
Same as Jeff, I'm not sure what do you mean by employee having several EmpId's, however, your current query

SELECT Distinct a.*,
b.Station,
c.EmpID
FROM #NoStaffID as a
Left JOIN #Station b
On a.City = b.City
JOIN Employee.Payroll c
On a.City=c.City
WHERE c.PayID is not null


will only return records where PayID is present and not null.

I have one question and one suggetsion to you:
1. Why there is no join on EmpID to Payroll table? Surely you may have multiple Payroll records per city, looks like "cut & paste" issue to me.
2. If you follow the order of JOINS from INNER to OUTER and use a bit better formatting, you will make your query much more readable and therefore more maintainable:



SELECT DISTINCT a.*
,b.Station
,c.EmpID
FROM #NoStaffID AS a
INNER JOIN Employee.Payroll AS c
ON c.City = a.City
LEFT JOIN #Station AS b
ON b.City = a.City
WHERE c.PayID IS NOT NULL



_____________________________________________
"The only true wisdom is in knowing you know nothing"
"O skol'ko nam otkrytiy chudnyh prevnosit microsofta duh!":-D
(So many miracle inventions provided by MS to us...)

How to post your question to get the best and quick help
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search