Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Joining to a single record in a child record
Message
 
 
General information
Forum:
Microsoft SQL Server
Category:
SQL syntax
Miscellaneous
Thread ID:
00948927
Message ID:
00948950
Views:
14
Fred,

Try
select cs.*, AH.*  
	from customer cs 
	LEFT OUTER JOIN activityhistory AH
		on AH.customerid = cs.customerid
			AND ah.inserteddatetime IN ( SELECT MAX(inserteddatetime) 
                           from activityhistory AH2
                           where AH.customerid = ah2.customerid)
>Okay I have a customer table with a child table with multiple records.
>
>I need to select data from the parent table and join it the last record from the child table based upon a datetime field.
>
>Something like this but without the error.
>select customerid, AH.*  from customer
>	LEFT OUTER JOIN (select top 1 customerid, agentid, inserteddatetime
>                           from activityhistory AH
>                           where AH.customerid = customer.customerid
>                           order by inserteddatetime desc )
>		on AH.customerid = customer.customerid
>
>The Error is:
>Server: Msg 156, Level 15, State 1, Line 6
>Incorrect syntax near the keyword 'on'.
>
>I know it can be done, I've seen it before but I just can't remember where.
--sb--
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform