Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
Discussion Groups
DB Engine
SQL ServerMSDESQL Server CE
Services
Analysis (Data Mining)Analysis (OLAP)DTSIntegration ServicesNotification ServicesReporting Services
Programming
CLRConnectivitySQLXML
Other Technologies
ClusteringEnglish QueryFull-Text SearchReplicationService Broker
General
Data WarehousingPerformanceSecuritySetupSQL Server ToolsOther SQL Server Topics
DirectoryUser Groups
Related Topics
MS AccessOther DB ProductsMS Server Products.NET DevelopmentVB DevelopmentJava DevelopmentMore Topics ...

SQL Server Forum / General / Other SQL Server Topics / January 2007

Tip: Looking for answers? Try searching our database.

Help With complex SELECT

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
SJ - 31 Jan 2007 00:00 GMT
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
SJ - 31 Jan 2007 01:49 GMT
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

> Can someone help me with an advanced query?
>
[quoted text clipped - 18 lines]
> Anyone db guru's out there?
> -SJ
Roy Harvey - 31 Jan 2007 04:41 GMT
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

>OK, I am close.
>
[quoted text clipped - 34 lines]
>> Anyone db guru's out there?
>> -SJ
SJ - 31 Jan 2007 18:00 GMT
> OK, I am close.
>
[quoted text clipped - 36 lines]
>
> - Show quoted text -

Thank you very very much!
Roy Harvey - 31 Jan 2007 18:12 GMT
>Thank you so much, This is perfect. However, when I do   dbUID =
>result.Fields.Item("uid")  always get a null
[quoted text clipped - 3 lines]
>
>Thanks again.

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
Beacon Falls, CT
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.