Hi everyone! I'm getting an error with this query:
Code:
select * from
(
	select
		avg(esiti.num_record) as media, count(*) as num_esiti
	from
		c_esiti esiti
	inner join
		(
			select 
				distinct dt_rif
			from
				c_esiti es
			where 
				es.r_id_controllo = @id
				and es.fl_successo = 1
				and es.dt_rif <>
				(
					select max(esi.dt_rif) from c_esiti esi
					where esi.fl_successo = 1 and esi.r_id_controllo = @id
				) 
		) esiti_ok
	on
		esiti.dt_rif = esiti_ok.dt_rif
		and esiti.dt_elab =
		(
			select 
				max(dt_elab)
			from
				c_esiti e
			where
				e.dt_rif = esiti_ok.dt_rif
		)
) esiti_precedenti
The funny thing is that the inner query (the one that produces "esiti_precedenti") works fine, but when I try to use it as a subquery I get "Internal SQL Server Error". The query produces a single row with an average and a count; I would need to append these two values to a row I get from another query, so my approach was to join them "on 1=1". Can you suggest an alternate way of doing it?

I'm using SQL Server 2000 in Query Analyzer.