sql server - SQL UPDATE statement combining multiple rows into one column -
i'm working airline data, have query this
select invoices.pnr, segments.depart, segments.arrival, segments.departdatetime invoices inner join segments s on i.invoice_id = s.invoice_id` pnr = 'aaaaaa'
this returns
pnr depart arrival departdatetime aaaaaa dfw mci 7/2/2012 7:30 aaaaaa mci lax 7/2/2012 11:30 aaaaaa lax dfw 7/4/2012 2:30 pm
i have column in invoices called routing want show 'dfw-mci-lax-dfw' possible using sql method ? segments listed in order, dfw-mci first mci-lax lax-dfw.
edit: if update database dfw-mci-mci-lax-lax-dfw acceptable. can strip out duplicate entries on view layer.
i can write in coldfusion, looping , thousands of database updates takes forever. mass update every 100 records, i'd avoid using other sql altogether
here's how can value. agree @automatic whether should update database data. since have recalculate every time of rows change.
;with x ( select i.pnr, i.invoice_id, s.depart, s.arrival, s.departdatetime, rn = row_number() on (partition i.pnr order s.departdatetime) dbo.invoices inner join dbo.segments s on i.invoice_id = s.invoice_id i.pnr = 'aaaaaa' ) select x.pnr, x.invoice_id, routing = (select case when rn1 = 1 depart else '' end + '-' + arrival x x2 x.pnr = x2.pnr , x.invoice_id = x2.invoice_id order x2.departdatetime xml path('') ) x group x.pnr, x.invoice_id;
Comments
Post a Comment