Hello! I need some help please..

i searched a lot, and tried everything... can someone help me please..

here's the deal:

i have this select:

SELECT * 
FROM localizacao, cliente, dispositivo                         
WHERE disp_id = clien_disp_id AND disp_id = local_disp_id 
ORDER BY local_id desc

it's output:
http://i.imagehost.org/0746/tabela.jpg

but what i really want is:

i want to select my client's, but where local_id is the last id i receive.. but i don't wanna show repetitive client's, i just want to show the last local_id for each client....
i want like to show only this ones: http://i.imagehost.org/0883/tabela2.jpg in the red circle

you know what i mean?
it's like two order by's but not showing repetitive client's... i tried distinct... but maybe im making it wrong..

help :s

thanks a lot

Member Avatar for spthorn

This was a good exercise! This code will do what you want:

select #localizacao.*, #cliente.*, #dispositivo.*
FROM #dispositivo
join #localizacao on #localizacao.local_disp_id = #dispositivo.disp_id
join #cliente on #cliente.clien_disp_id = #dispositivo.disp_id
join (
	select max(local_id) as localid, clien_id, local_disp_id
	FROM #localizacao, #cliente, #dispositivo                         
	WHERE disp_id = clien_disp_id AND disp_id = local_disp_id 
	group by local_disp_id, clien_id
) as a
on a.localid=#localizacao.local_id and a.clien_id=#cliente.clien_id and a.local_disp_id=#dispositivo.disp_id

Thank you so mutch spthorn!!!!!!

THANK YOU MY FRIEND, THANKS FOR YOUR HELP, IT WORKED! :D

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.