Question : Loop through TOP1 SQL query in ASP

I have a query that I can't seem to get the results I want. I'm using ASP and a SQL statement (Access Database). I have a CLIENT table that contains clients, and also an ACTIVITY tavle that tracks communications with clients. I'm basically displaying a list of result of each client and their latest activity and activity date. Finally, I want to put the result in a variable. Here is way I have so far:

sql = "Select *, (Select Top 1 Activity, ActivityDate from ACTIVITY Left Join CLIENT on CLIENT.CLIENTID = ACTIVITY.CLIENTID) FROM CLIENT"
sql = sql & " order by CLIENT.CLIENTNAME"
set rs= conn.execute(sql)

ClientiD = rs("ClientID")      
ClientName = rs("ClientName")      
LastActivity = rs("Activity")      
LastActivityDate = rs("ActivityDate")

Loop stuff here....

Is my query correct? I only get one Client per row like I would like but I can't get the data in the "LastActivity" variable.

Any help? Thanks.

Brian

Answer : Loop through TOP1 SQL query in ASP

You are right. What I failed to do is pull out the max activity date for each client first then join in the information for each record from that. Give this one a try:
SELECT Q1.LastActivityDate, Q1.ClientID, CLIENT.ClientName, ACTIVITY.Activity
    FROM ACTIVITY, CLIENT,
    (SELECT Max(ACTIVITY.ActivityDate) AS LastActivityDate, ACTIVITY.ClientID
    FROM ACTIVITY
    GROUP BY ACTIVITY.ClientID) Q1
    WHERE Q1.LastActivityDate = ACTIVITY.LastActivityDate
         AND Q1.ClientID = ACTIVITY.ClientID
         AND Q1.ClientID =  CLIENT.ClientID
    ORDER BY CLIENT.ClientName
Random Solutions  
 
programming4us programming4us