all groups > sql server (alternate) > january 2007 >
You're in the

sql server (alternate)

group:

Help With complex SELECT



Help With complex SELECT SJ
1/30/2007 4:00:24 PM
sql server (alternate): Can someone help me with an advanced query?

I have two tables

Table 1: Employees
Cols: UID, lname, fname

Table 2: StatEntry
Cols: UID, Timestamp, description

The queary should reaturn all the information in both tables. If more
than one entry exists in the second table, it should return the one
with the greatest timestamp. If not entries exist I would like the
second table columns set to "no value"

Something link:
select Employees.*,StatEntry.* from Employees JOIN StatEntry ON
employees.uid == statentry.uid WHERE timestamp in (select
MAX(timestamp) from statentry where uid=employees.uid).

Anyone db guru's out there?
-SJ
Re: Help With complex SELECT SJ
1/30/2007 5:49:11 PM
OK, I am close.

Here is what I have

SELECT a.*,b.* from employees as a LEFT JOIN statentry as b ON
a.uid=b.uid
WHERE b.timestamp IN (select MAX(timestamp) from statentry where
uid=b.uid)
OR b.timestamp IS NULL


This returns me all the values, but for some rease UID in the result
set is allways null. ANy idea why?

THanks,
-SJ

[quoted text, click to view]

Re: Help With complex SELECT Roy Harvey
1/31/2007 4:41:06 AM
You could try something like:

SELECT a.*, b.*
FROM employees as a
LEFT OUTER
JOIN (SELECT *
FROM statentry as X
WHERE X.timestamp =
(select max(timestamp)
from statentry as Y
where X.uid = Y.uid)) as b
ON a.uid = b.uid

Roy Harvey
Beacon Falls, CT

[quoted text, click to view]
Re: Help With complex SELECT SJ
1/31/2007 10:00:39 AM
[quoted text, click to view]

Thank you very very much!
Re: Help With complex SELECT Roy Harvey
1/31/2007 1:12:52 PM
[quoted text, click to view]

Try removing the quotes. Use result.Fields.Item(uid) rather than
result.Fields.Item("uid"), result.Fields.Item(a.uid) rather than
result.Fields.Item("a.uid").

Roy Harvey
AddThis Social Bookmark Button