Query question

Please support our MS SQL advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Jul 2008
Posts: 24
Reputation: Link82 is an unknown quantity at this point 
Solved Threads: 0
Link82 Link82 is offline Offline
Newbie Poster

Query question

 
0
  #1
Jul 21st, 2009
Hi Guys,

Kind of stuck on a query here.

  1. SELECT *--, c.last_name + ', ' + c.first_name as name
  2. FROM dbo.tblLevelOneApprover a
  3.  
  4. INNER JOIN dbo.tblLevelTwoApproverToLevelOneApprover b
  5. ON convert(int, b.level_two_emplid) != convert(int, @emplid)
  6.  
  7. WHERE convert(int, a.emplid) = convert(int, b.level_one_emplid)

I'm trying to get all level one managers who are not mapped to a particular level 2 manager. For example, let's call this table dbo.tblLevelTwoApproverToLevelOneApprover with following entries:

level_two_emplid | level_one_emplid
stan | susan
al | freddy
max | susan
karen | jeff
bob | freddy

So what I'm trying to do is, when I try to run the above query with stan's id, I should get back only jeff and freddy (people who are not mapped to him). Another example is if we run bob, we should get back jeff and susan.

In the above query, I have the line on convert(int, b.level_two_emplid) != convert(int, @emplid)
which minuses susan once, but since she's repeated with another level 2 manager, her name still shows up. So how do I say don't return me the names of the people that you map to.

Am I missing something ridiculously simple here?
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 3,215
Reputation: sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of 
Solved Threads: 573
Sponsor
sknake's Avatar
sknake sknake is offline Offline
.NET Enthusiast

Re: Query question

 
0
  #2
Jul 21st, 2009
You're taking the wrong approach.. Conceptually you should be doing
  1. DECLARE @level2mgrId INT
  2. SET @level2mgrId = 5
  3.  
  4. SELECT *
  5. FROM Level1
  6. WHERE
  7. NOT EXISTS
  8. (
  9. SELECT *
  10. FROM Level2
  11. WHERE Level2.ParentManagerId = Level1.ManagerId AND Level2.ManagerId = @level2mgrId
  12. )

Let me know if that wasn't what you were after
Scott Knake
Custom Software Development
Apex Software, Inc.
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 118
Reputation: cgyrob is on a distinguished road 
Solved Threads: 18
cgyrob's Avatar
cgyrob cgyrob is offline Offline
Junior Poster

Re: Query question

 
1
  #3
Jul 21st, 2009
I don't see where you are getting the alias 'c'

Besides that i think you are making it more difficult then it has to be.


  1. SELECT *
  2. FROM dbo.tblLevelOneApprover a, dbo.tblLevelTwoApproverToLevelOneApprover b
  3. WHERE convert(INT, b.level_two_emplid) <> convert(INT, a.emplid)
Last edited by cgyrob; Jul 21st, 2009 at 6:20 pm.
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 3,215
Reputation: sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of 
Solved Threads: 573
Sponsor
sknake's Avatar
sknake sknake is offline Offline
.NET Enthusiast

Re: Query question

 
0
  #4
Jul 21st, 2009
cgyrob -- Check out these threads:
http://www.daniweb.com/forums/thread203649.html
http://www.daniweb.com/forums/thread202905.html

They give a little more background on the database
Last edited by sknake; Jul 21st, 2009 at 6:22 pm.
Scott Knake
Custom Software Development
Apex Software, Inc.
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 118
Reputation: cgyrob is on a distinguished road 
Solved Threads: 18
cgyrob's Avatar
cgyrob cgyrob is offline Offline
Junior Poster

Re: Query question

 
0
  #5
Jul 21st, 2009
@sknake - thanks, I didn't realize there were multiple threads relating to this db.
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 3,215
Reputation: sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of 
Solved Threads: 573
Sponsor
sknake's Avatar
sknake sknake is offline Offline
.NET Enthusiast

Re: Query question

 
0
  #6
Jul 21st, 2009
I wouldn't have either had I not participated
Scott Knake
Custom Software Development
Apex Software, Inc.
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 118
Reputation: cgyrob is on a distinguished road 
Solved Threads: 18
cgyrob's Avatar
cgyrob cgyrob is offline Offline
Junior Poster

Re: Query question

 
0
  #7
Jul 21st, 2009
I actually remember reading both of these threads and had commented on one of them but I still did not correlate them.
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 24
Reputation: Link82 is an unknown quantity at this point 
Solved Threads: 0
Link82 Link82 is offline Offline
Newbie Poster

Re: Query question

 
0
  #8
Jul 22nd, 2009
Hi sknake and cgyrob,

Thanks for the help. I apologize for no additional details. My head was too into it and I should have looked at the post after clearing it.

But sknake knew exactly wh at I needed! And yes, not exists is what I should've considered. Can't believe I wasn't even thinking using 'not exists'. Don't think I've had to so far, but learning new stuff, so that's cool
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC