Microsoft SQL Server
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
 
User Name:
Password:
Remember me
Go Back   ASP Free ForumsDatabaseMicrosoft SQL Server

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread ASP Free Forums Sponsor:
  #1  
Old August 25th, 2004, 05:08 PM
dcampos's Avatar
dcampos dcampos is offline
Contributing User
ASP Free Newbie (0 - 499 posts)
 
Join Date: Aug 2004
Posts: 234 dcampos User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 Days 7 h 54 m 13 sec
Reputation Power: 5
Talking SQL Question

I need some help with an SQL statement. I have two tables. Emp_Users and Emp_UserJob. Both of these tables are connected by a UserID. Each time an employee changes their employment status, they receive a new UserJobID number. Also, they have several different leadership levels. What I am trying to do is view the latest leadership levels that are equal to leadershiplevel 20. What I need to do is select the MAX UserJobId and then select all of the employees that have a leadership level of 20. This is what I have right now:

SELECT DISTINCT U.UserID, U.FirstName, U.LastName, U.MiddleName, U.UserID, J.LeadershipLevel, U.EmploymentStatus, J. UserJobID
FROM Emp_Users As U INNER JOIN Emp_UserJob As J on (U.UserID=J.UserID)
WHERE J.UserJobID = (SELECT MAX(UserJobID)
FROM Emp_UserJob) AND J.LeadershipLevel = 20

I believe that all this will show me is one employee with the highest UserJobID with the LeadershipLevel of 20 when I need to see all employees' highest UserJobID with the leadershiplevel of 20. I appreciate any help.

Reply With Quote
  #2  
Old August 25th, 2004, 05:18 PM
Memnoch's Avatar
Memnoch Memnoch is offline
Unholy Moderator
Click here for more information.
 
Join Date: Oct 2003
Location: In hell, where did you think?
Posts: 11,781 Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level) 
Time spent in forums: 3 Weeks 5 Days 8 h 45 m 55 sec
Reputation Power: 470
Post a sampling of your database data and a sample of what you are expecting the query to return and I will try to help you.

Reply With Quote
  #3  
Old August 25th, 2004, 05:36 PM
dcampos's Avatar
dcampos dcampos is offline
Contributing User
ASP Free Newbie (0 - 499 posts)
 
Join Date: Aug 2004
Posts: 234 dcampos User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 Days 7 h 54 m 13 sec
Reputation Power: 5
Here is a sample on employees information: Sorry about the formatting.


UserJobID - LeadershipLevel
667 - 0
2139 - 20
2140 - 20
2141 - 20
2364 - 0
2365 - 0
2366 - 20


In this case, I want the statement to look at the max UserJobID (2366) and then compare it to the LeadershipLevel that corresponds with it. (20) If the Highest UserJobID has a LeadershipLevel of 20, then I want to see their information like First, Last, Middle names, etc. The SQL statement should ignore the other userJobID's since I want it to compare the MAX UserJobID. At first I was just selecting all records that in which LeadershipLevel = 20, but there may be an instance when previous UserJobId's = 20 but the MAX UserJobID would = 30. It would still show me that employees record since there was a 20 in the LeadershipLevel somewhere along the way. I'm basically trying to weed out the most current records that = 20.

LastName FirstName MiddleName LeadershipLevel

Doe John M 20

Reply With Quote
  #4  
Old August 25th, 2004, 05:39 PM
Memnoch's Avatar
Memnoch Memnoch is offline
Unholy Moderator
Click here for more information.
 
Join Date: Oct 2003
Location: In hell, where did you think?
Posts: 11,781 Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level) 
Time spent in forums: 3 Weeks 5 Days 8 h 45 m 55 sec
Reputation Power: 470
I meant sample data for both tables.

Reply With Quote
  #5  
Old August 25th, 2004, 05:41 PM
Memnoch's Avatar
Memnoch Memnoch is offline
Unholy Moderator
Click here for more information.
 
Join Date: Oct 2003
Location: In hell, where did you think?
Posts: 11,781 Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level) 
Time spent in forums: 3 Weeks 5 Days 8 h 45 m 55 sec
Reputation Power: 470
Have you tried.
Code:
SELECT A.FirstName, A.LastName, A.MiddleName, B.LeadershipLevel
FROM Emp_Users As A 
INNER JOIN Emp_UserJob As B On (A.UserID = B.UserID)
WHERE A.UserID IN
(
   SELECT DISTINCT UserID
   FROM Emp_UserJob
   WHERE LeadershipLevel = 20
)

Reply With Quote
  #6  
Old August 25th, 2004, 05:49 PM
dcampos's Avatar
dcampos dcampos is offline
Contributing User
ASP Free Newbie (0 - 499 posts)
 
Join Date: Aug 2004
Posts: 234 dcampos User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 Days 7 h 54 m 13 sec
Reputation Power: 5
Will this grab the highest or most recent UserJobID? I guess that I was assuming that I would need to use the MAX function in the statement.

Reply With Quote
  #7  
Old August 25th, 2004, 06:01 PM
dcampos's Avatar
dcampos dcampos is offline
Contributing User
ASP Free Newbie (0 - 499 posts)
 
Join Date: Aug 2004
Posts: 234 dcampos User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 Days 7 h 54 m 13 sec
Reputation Power: 5
EmpUsers
UserID - LastName - FirstName - MiddleName
1 John
2 Mary
3 Tom
4 David
5 Mike


EmpUserJob
UserID - UserJobID - LeadershipLevel
1 - 667 - 0
1 - 2139 - 20
1 - 2140 - 20
1 - 2141 - 20
1 - 2364 - 0
1 - 2365 - 0
1 - 2366 - 20

Does This help any?

Reply With Quote
Reply

Viewing: ASP Free ForumsDatabaseMicrosoft SQL Server > SQL Question


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 3 hosted by Hostway
Stay green...Green IT