943,955 Members | Top Members by Rank

Ad:
  • MS SQL Discussion Thread
  • Marked Solved
  • Views: 532
  • MS SQL RSS
Jul 21st, 2009
0

Query question

Expand Post »
Hi Guys,

Kind of stuck on a query here.

MS SQL Syntax (Toggle Plain Text)
  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?
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Link82 is offline Offline
24 posts
since Jul 2008
Jul 21st, 2009
0

Re: Query question

You're taking the wrong approach.. Conceptually you should be doing
sql Syntax (Toggle Plain Text)
  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
Featured Poster
Reputation Points: 1749
Solved Threads: 735
Senior Poster
sknake is offline Offline
3,948 posts
since Feb 2009
Jul 21st, 2009
1

Re: Query question

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.


sql Syntax (Toggle Plain Text)
  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.
Reputation Points: 91
Solved Threads: 18
Junior Poster
cgyrob is offline Offline
125 posts
since Sep 2008
Jul 21st, 2009
0

Re: Query question

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.
Featured Poster
Reputation Points: 1749
Solved Threads: 735
Senior Poster
sknake is offline Offline
3,948 posts
since Feb 2009
Jul 21st, 2009
0

Re: Query question

@sknake - thanks, I didn't realize there were multiple threads relating to this db.
Reputation Points: 91
Solved Threads: 18
Junior Poster
cgyrob is offline Offline
125 posts
since Sep 2008
Jul 21st, 2009
0

Re: Query question

I wouldn't have either had I not participated
Featured Poster
Reputation Points: 1749
Solved Threads: 735
Senior Poster
sknake is offline Offline
3,948 posts
since Feb 2009
Jul 21st, 2009
0

Re: Query question

I actually remember reading both of these threads and had commented on one of them but I still did not correlate them.
Reputation Points: 91
Solved Threads: 18
Junior Poster
cgyrob is offline Offline
125 posts
since Sep 2008
Jul 22nd, 2009
0

Re: Query question

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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Link82 is offline Offline
24 posts
since Jul 2008

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in MS SQL Forum Timeline: Set Warnings Off
Next Thread in MS SQL Forum Timeline: Just a bit of help required, newbie here





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC