Hi there,I am trying to join my main set to an aggregated set(top part). Please see code below:select concat(subquery.pataccountnumber, subquery.last_visit) test2 from(SELECT a.[PatAccountNumber] ,max(b.Sequence) last_Visit FROM [Visits] as a inner join [ServiceChange] as b on a.URN = b.URN and a.SYS_SystemID = b.SYS_SystemID where a.DisTimeDateTime between '12-01-2014' and '01-06-2015' and a.Fac in ('AAA') group by a.PatAccountNumber) as subquery inner join (SELECT a.[PatAccountNumber] ,b.Sequence ,CONCAT(a.PatAccountNumber, b.Sequence) as linked_keyyy FROM [Visits] as a inner join [ServiceChange] as b on a.URN = b.URN and a.SYS_SystemID = b.SYS_SystemID where a.DisTimeDateTime between '12-01-2014' and '01-06-2015' and a.Fac in ('AAA')) as subquery2 on subquery.test2 = subquery2.linked_keyyy
It fails by saying test2 is an invalid column. I wonder if I am not representing the first aggregated set as a table properly.Can anyone help?Thanks