Monday, 15 March 2010

sql - Group by or distinct - how can I do for 3 tables -



sql - Group by or distinct - how can I do for 3 tables -

select c.urun_id, a.kat_adi, b.resim_yolu, b.sira, c.urun_sira, c.urun_adi kategoriler a,resimler b, urunler c a.kat_id=c.kat_id , c.urun_id=b.urun_id order c.urun_sira,b.sira

there 2 record urun_id = 17. want homecoming 1 record. have category, products, images tables. user has added 2 photos in images table urun_id=17.

but want show 1 record has minimum value in sira.

if output of query

25 ~ pc ~ 3 17 ~ phone ~ 2 17 ~ phone ~ 4

then, want return

17 ~ phone ~ 2

in sqlserver2005+ utilize row_number function

;with cte ( select c.urun_id, a.kat_adi, b.resim_yolu, b.sira, c.urun_sira, c.urun_adi, row_number() on (partition c.urun_id order b.sira) rn kategoriler bring together urunler c on a.kat_id=c.kat_id bring together resimler b on c.urun_id=b.urun_id ) select * cte rn = 1 order urun_sira, sira

other way

select c.urun_id, a.kat_adi, b.resim_yolu, b.sira, c.urun_sira, c.urun_adi kategoriler bring together urunler c on a.kat_id=c.kat_id bring together resimler b on c.urun_id=b.urun_id exists ( select 1 resimler r b.urun_id = r.urun_id having min(r.sira) = b.sira ) order c.urun_sira, b.sira

sql image group-by sql-order-by

No comments:

Post a Comment