la forma correcta de utilizar como máximo en la cláusula teniendo es mediante la realización de una auto se unen en primer lugar:
select t1.a, t1.b, t1.c
from table1 t1
join table1 t1_max
on t1.id = t1_max.id
group by t1.a, t1.b, t1.c
having t1.date = max(t1_max.date)
la siguiente es la forma de se uniría con un subquer Y:
select t1.a, t1.b, t1.c
from table1 t1
where t1.date = (select max(t1_max.date)
from table1 t1_max
where t1.id = t1_max.id)
Asegúrese de crear un único conjunto de datos antes de utilizar un agregado cuando se trata de un multi-unión de tablas:
select t1.id, t1.date, t1.a, t1.b, t1.c
into #dataset
from table1 t1
join table2 t2
on t1.id = t2.id
join table2 t3
on t1.id = t3.id
select a, b, c
from #dataset d
join #dataset d_max
on d.id = d_max.id
having d.date = max(d_max.date)
group by a, b, c
Sub versión de consulta:
select t1.id, t1.date, t1.a, t1.b, t1.c
into #dataset
from table1 t1
join table2 t2
on t1.id = t2.id
join table2 t3
on t1.id = t3.id
select a, b, c
from #dataset d
where d.date = (select max(d_max.date)
from #dataset d_max
where d.id = d_max.id)
No soy un gurú de SQL pero ¿funciona o no? Ni siquiera declara con qué tiene problemas y si su solución publicada funciona o no. –
Vas a tener que explicarme lo que quieres para que yo entienda esta pregunta. – tster
¿hay alguna solución para obtener el resultado correcto? –