here tables:
----------------------------------------- hotels ----------------------------------------- hotelid | hotelname ----------------------------------------- ----------------------------------------- hotelimages ----------------------------------------- hotelimageid | hotelid | filename -----------------------------------------
i'd create select statement return data so:
hotelid | hotelname | images ----------------------------------------- 1 | hotel1 | image1,image2,image3 2 | hotel2 | image4,image5,image6
how can modify query this? have:
select h.hotelid, h.hotelname, '' images hotels h
i know can use coalesce create comma-delimited list:
declare @images varchar(500) select @images = coalesce(@images + ',', '') + cast(filename varchar(100)) hotelimages hotelid = 1 select @images
but don't know how integrate current query list can returned rest of hotel data.
i should mention i'm using sql server 2000.
since sql 2000 options limited. can't use xml path or recursive ctes
also coalesce trick works stuffing comma delimited list single hotel. not of them.
honestly you're better off doing in client. if can't done (certain report apps that-which-must-not-be-named come mind) can following.
note number of times loop process equal maximum number of files associated hotel. better setting loop each hotel (e.g. calling udf select clause).
declare @foo table ( hotelid int , lastid int , filenamelist varchar(8000)) insert @foo select start.hotelid, start.firstid , id.filename (select hotelid, min(hotelimageid ) firstid hotelimages group hotelid) start inner join hotelimages hi on start.firstid = hi.hotelimageid while @@rowcount <> 0 begin update @foo set filenamelist = filenamelist + ',' + filename lastid = xnext.nextid @foo f inner join hotelimages hi on f.hotelid = id.hotelid inner join (select id.hotelid, min(hotelimageid ) nextid hotelimages hi inner join @foo f on f.hotelid = id.hotelid id.hotelimageid > f.lastid group id.hotelid) xnext on xnext.nextid = id.hotelimageid end select h.hotelid h.hotelname, f.filenamelist hotels h inner join @foo f on h.hotelid = f.hotelid
Comments
Post a Comment