?
Solved

SQL - Help with a Query

Posted on 2012-08-29
3
Medium Priority
?
553 Views
Last Modified: 2012-08-29
I have an employees table (PS_EMPLOYEES) from which I'm pulling the following
- employeeID (primary key)
- name
- user name
- title

I have a job table (PS_JOBS) from which I'm pulling the following
- employeeID (primary key)
- FileNumber

When the results come back, some employees are listed twice, because in the jobs table they have more then one file number. I need the record which has the larger file number. How would I adjust the query below to do that? Thanks!

SELECT distinct    PS_EMPLOYEES.EMPLID, PS_EMPLOYEES.NAME, PS_EMPLOYEES.FIRST_NAME, PS_EMPLOYEES.MIDDLE_NAME, PS_EMPLOYEES.LAST_NAME,
                      PS_EMPLOYEES.BUSINESS_TITLE, PS_JOB.FILE_NBR
FROM         PS_EMPLOYEES LEFT OUTER JOIN
                      PS_JOB ON PS_EMPLOYEES.EMPLID = PS_JOB.EMPLID
0
Comment
Question by:anglandp
3 Comments
 
LVL 13

Accepted Solution

by:
LIONKING earned 2000 total points
ID: 38347077
You could use a MAX and a group by to do this.
Something like:

SELECT PS_EMPLOYEES.EMPLID, PS_EMPLOYEES.NAME, PS_EMPLOYEES.FIRST_NAME, PS_EMPLOYEES.MIDDLE_NAME, PS_EMPLOYEES.LAST_NAME,
                      PS_EMPLOYEES.BUSINESS_TITLE, MAX(PS_JOB.FILE_NBR) FILE_NBR
FROM         PS_EMPLOYEES LEFT OUTER JOIN
                      PS_JOB ON PS_EMPLOYEES.EMPLID = PS_JOB.EMPLID
GROUP BY PS_EMPLOYEES.EMPLID, PS_EMPLOYEES.NAME, PS_EMPLOYEES.FIRST_NAME, PS_EMPLOYEES.MIDDLE_NAME, PS_EMPLOYEES.LAST_NAME,
                      PS_EMPLOYEES.BUSINESS_TITLE


*EDIT* Removed DISTINCT
0
 
LVL 61

Expert Comment

by:HainKurt
ID: 38347549
use row_number

select * from (
SELECT PS_EMPLOYEES.EMPLID, PS_EMPLOYEES.NAME, PS_EMPLOYEES.FIRST_NAME, PS_EMPLOYEES.MIDDLE_NAME, PS_EMPLOYEES.LAST_NAME, 
                      PS_EMPLOYEES.BUSINESS_TITLE, PS_JOB.FILE_NBR,
row_number() over(partition by PS_EMPLOYEES.EMPLID order by FILE_NUMBER desc) rn
FROM         PS_EMPLOYEES LEFT OUTER JOIN
                      PS_JOB ON PS_EMPLOYEES.EMPLID = PS_JOB.EMPLID
) x where rn=1

Open in new window

0
 

Author Closing Comment

by:anglandp
ID: 38347581
this did it perfectly. Thank you kindly!
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A Stored Procedure in Microsoft SQL Server is a powerful feature that it can be used to execute the Data Manipulation Language (DML) or Data Definition Language (DDL). Depending on business requirements, a single Stored Procedure can return differe…
It is possible to export the data of a SQL Table in SSMS and generate INSERT statements. It's neatly tucked away in the generate scripts option of a database.
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question