A simply but complete query is generally "SELECT columnNames FROM TableName". I don't see the table in the inner query. if you say its working fine then okay .. then the error could be due to the "Partition by Typegroup order by DateTime2 asc". SQL is unable to find this column. but still I wonder how could you run an inner join without specifying a table name in the inner query.