I am having some trouble figuring out the correct syntax for what I need to output from my query.
Here is my data (no these are not the correct column names, I'm trying to make it easier to read) The actual table I'm pulling from has 26 columns of data.
employeeNumber - recordNumber - job - dept - type
12 - 1 - stapler - 788 - s
12 - 6 - paper pusher - 400 - s
18 - 2 - phone cleaner - 600 - p
18 - 4 - sweeper - 567 - s
19 - 0 - typist - 400 - s
21 - 0 - mouse ball cleaner - 400 - p
So, here are the selection rules:
If type is P select that record
If employee has no P record, select S
If employee has multiple S records, select the lower record number OR select the record that the Dept is NOT 400 (the 400 row will always be the higher record number)
If there is only one S record, and the Dept is 400 the row should be returned
This is what is currently working:
SELECT employeeNumber, recordNumber, job, dept, type
FROM employees
WHERE (type = 'P')
OR
(type = 'S'
AND employeeNumber NOT IN
(
SELECT employeeNumber
FROM employees
WHERE type = 'P'
)
)
ORDER BY employeeNumber, recordNumber
And what I WANT to do is put "limit = 1" (or something similar) at the end, so that in the case of multiple S rows the lower recordNumber will be the row that is returned.
This is the record set that should be returned from the data above:
employeeNumber - recordNumber - job - dept - type
12 - 1 - stapler - 788 - s
18 - 2 - phone cleaner - 600 - p
19 - 0 - typist - 400 - s
21 - 0 - mouse ball cleaner - 400 - p
Clear as mud, right?
I keep getting errors. Is there an "easy" way to do it that I'm just missing?
Thanks for any help you can give.
Michelle