SQL Server 2008 combines two queries

I missed something trivial here. This query is not for any reason, in particular, other than trying to practice combining the two queries. The errors I get

Msg 156, Level 15, State 1, Line 10 Incorrect syntax near the keyword 'inner'. Msg 156, Level 15, State 1, Line 16 Incorrect syntax near the keyword 'as'. 

And request

 select t.countyName, x.countyName from ( SELECT DISTINCT Patient.patientid, tblStateCounties.countyName, Patient.countyCode FROM Patient INNER JOIN tblStateCounties ON Patient.stateCode = tblStateCounties.stateCode AND Patient.countyCode = tblStateCounties.countyCode WHERE (Patient.patientage > 80) ) inner join ( SELECT DISTINCT Patient.patientid, tblStateCounties.countyName, Patient.countyCode FROM Patient INNER JOIN tblStateCounties ON Patient.stateCode = tblStateCounties.stateCode AND Patient.countyCode = tblStateCounties.countyCode WHERE (Patient.patientage < 80) ) as x on t.countyname=x.countyname 
+4
source share
3 answers

You forgot to use an alias in the first subquery .

+6
source
 select t.countyName, x.countyName from ( SELECT DISTINCT Patient.patientid, tblStateCounties.countyName, Patient.countyCode FROM Patient INNER JOIN tblStateCounties ON Patient.stateCode = tblStateCounties.stateCode AND Patient.countyCode = tblStateCounties.countyCode WHERE (Patient.patientage > 80) ) rsT inner join ( SELECT DISTINCT Patient.patientid, tblStateCounties.countyName, Patient.countyCode FROM Patient INNER JOIN tblStateCounties ON Patient.stateCode = tblStateCounties.stateCode AND Patient.countyCode = tblStateCounties.countyCode WHERE (Patient.patientage < 80) ) rsX on rsT.countyname=rsX.countyname 
+3
source

using

 ( SELECT DISTINCT Patient.patientid, tblStateCounties.countyName, Patient.countyCode FROM Patient INNER JOIN tblStateCounties ON Patient.stateCode = tblStateCounties.stateCode AND Patient.countyCode = tblStateCounties.countyCode WHERE (Patient.patientage > 80) ) as t 
0
source

All Articles