TSQL - Select multiple rows into single row. (Master / detail tables)
Posted on 2008-06-18
I am trying to put together a query that summarises a month worth of items in a detail table and I'm struggling a bit. Basically what I want to have at the end is:
Item 1 - 15 - £10
Item 2 - 1 - £12
Covers deliveries 1232, 1234, 502. Dates 01/01/2000, 01/02/2000, 01/02/2000
The item rows are easy, but I'm not sure how to put together that final row. Tables being worked with are something like this:
I can do a simple select to return all detail rows required (SELECT [DeliveryNumber], [DeliveryDate] FROM [DetailTable] WHERE [SummaryID] = @SummaryID) but I need to get this into a single row and formatted as closely as I can to the sample above.