Need SQL Help

WannaFly

Platinum Member
Jan 14, 2003
2,811
1
0
table structure:
id numeric - PK
EmplID - nvarcar
plate - nvarchar
date - timestamp


Rows are only ever inserted, never modified or deleted. This is basicallly a lookup table. I want to fine what plates belong to certian emplids and what emplids belong to plates. When an association is broken, emplid is set to null.

I need a query that will tell me what plates an emplid owns currently. Seems easy. Ex:

emplid plate
123 qrs
123 abc
456 def
null abc
123 xyz

the query i need would return qrs and xyz but NOT abc because an entry exists with a null with a later timestamp. Instead of null it could be anything else too (if it was 456 instead of null, the query shouldnt return it)

kind of a longconfusing situation, but it seems like something so easy - either my brain isnt working or I just cant figure this one out. Let me know if i havent been clear enough, thanks.
 

oog

Golden Member
Feb 14, 2002
1,721
0
0
select top 1 emplid, plate from tablename where emplid = @input_emplid order by [date]

i don't remember if date should be ascending or descending to get what you want. essentially you ask for the first row that matches the emplid when the results are sorted by date. nulls won't match.

edit: sorry, top 1 won't work. i didn't read your post closely enough. i thought you were asking for the most recent one. off the top of my head, i would use a subquery to find the most recent date and match that in the where clause.
 

WannaFly

Platinum Member
Jan 14, 2003
2,811
1
0
oog: Im not sure how the subquery would work because the most recent date is differnet on every plate

shutterFly: that doesnt account for ones that have null associates, it just returns all that were ever associated with that emplid.
ie from my data above it would return qrs,abc,xyz even though abc should not be returned.

Thanks for the input though. Im still working on figuring it out, have no clue hah
 

shutterFly

Member
Nov 5, 2003
57
0
0
Originally posted by: WannaFly
oog: Im not sure how the subquery would work because the most recent date is differnet on every plate

shutterFly: that doesnt account for ones that have null associates, it just returns all that were ever associated with that emplid.
ie from my data above it would return qrs,abc,xyz even though abc should not be returned.

Thanks for the input though. Im still working on figuring it out, have no clue hah

Haha! You're right... brain fart on my part...

Anyway, this might do what you need:

SELECT emplid, plate, timestamp
FROM table t
WHERE t.timestamp =
(
SELECT MAX(timestamp)
FROM table
WHERE plate = t.plate
)
 

WannaFly

Platinum Member
Jan 14, 2003
2,811
1
0
Exactly what I needed, thanks ShutterFly! It was one of those things where I went "doh! it was that easy " .
 
sale-70-410-exam    | Exam-200-125-pdf    | we-sale-70-410-exam    | hot-sale-70-410-exam    | Latest-exam-700-603-Dumps    | Dumps-98-363-exams-date    | Certs-200-125-date    | Dumps-300-075-exams-date    | hot-sale-book-C8010-726-book    | Hot-Sale-200-310-Exam    | Exam-Description-200-310-dumps?    | hot-sale-book-200-125-book    | Latest-Updated-300-209-Exam    | Dumps-210-260-exams-date    | Download-200-125-Exam-PDF    | Exam-Description-300-101-dumps    | Certs-300-101-date    | Hot-Sale-300-075-Exam    | Latest-exam-200-125-Dumps    | Exam-Description-200-125-dumps    | Latest-Updated-300-075-Exam    | hot-sale-book-210-260-book    | Dumps-200-901-exams-date    | Certs-200-901-date    | Latest-exam-1Z0-062-Dumps    | Hot-Sale-1Z0-062-Exam    | Certs-CSSLP-date    | 100%-Pass-70-383-Exams    | Latest-JN0-360-real-exam-questions    | 100%-Pass-4A0-100-Real-Exam-Questions    | Dumps-300-135-exams-date    | Passed-200-105-Tech-Exams    | Latest-Updated-200-310-Exam    | Download-300-070-Exam-PDF    | Hot-Sale-JN0-360-Exam    | 100%-Pass-JN0-360-Exams    | 100%-Pass-JN0-360-Real-Exam-Questions    | Dumps-JN0-360-exams-date    | Exam-Description-1Z0-876-dumps    | Latest-exam-1Z0-876-Dumps    | Dumps-HPE0-Y53-exams-date    | 2017-Latest-HPE0-Y53-Exam    | 100%-Pass-HPE0-Y53-Real-Exam-Questions    | Pass-4A0-100-Exam    | Latest-4A0-100-Questions    | Dumps-98-365-exams-date    | 2017-Latest-98-365-Exam    | 100%-Pass-VCS-254-Exams    | 2017-Latest-VCS-273-Exam    | Dumps-200-355-exams-date    | 2017-Latest-300-320-Exam    | Pass-300-101-Exam    | 100%-Pass-300-115-Exams    |
http://www.portvapes.co.uk/    | http://www.portvapes.co.uk/    |