Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

Help with Joins..... Expand / Collapse
Author
Message
Posted Tuesday, July 9, 2013 3:24 PM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Tuesday, March 18, 2014 6:31 AM
Points: 23, Visits: 93
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
Post #1471883
Posted Tuesday, July 9, 2013 3:33 PM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Today @ 3:23 PM
Points: 13,272, Visits: 12,103
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)
Post #1471885
Posted Tuesday, July 9, 2013 7:11 PM


Hall of Fame

Hall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of Fame

Group: General Forum Members
Last Login: Yesterday @ 4:14 AM
Points: 3,618, Visits: 5,254
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!
Post #1471908
Posted Tuesday, July 9, 2013 10:27 PM


SSC-Dedicated

SSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-Dedicated

Group: General Forum Members
Last Login: Today @ 4:16 PM
Points: 36,944, Visits: 31,449
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."

(play on words) "Just because you CAN do something in T-SQL, doesn't mean you SHOULDN'T." --22 Aug 2013

Helpful Links:
How to post code problems
How to post performance problems
Post #1471941
Posted Wednesday, July 10, 2013 8:24 AM
SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Monday, August 18, 2014 8:36 AM
Points: 2,836, Visits: 5,066
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!"
(So many miracle inventions provided by MS to us...)

How to post your question to get the best and quick help
Post #1472181
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse