I have a table tblItems
with records having the same UniqueID
and Description
but with different dates
and amount
.
Now I want to show the records by grouping them together using the UniqueIDs
and then display the Amount
under a dynamically created Date
column using the same record.
Date range will be provided as parameter like start date
and end date
and based on the date range there might be N number of columns.
Described with the example….
tblItems table
╔════════════╦════════════════════╦══════════╦═════════╗
║ UNIQUECODE ║ DESCRIPTION ║ DATE ║ AMOUNT ║
╠════════════╬════════════════════╬══════════╬═════════╣
║ ABCD ║ Item Decsription 1 ║ 1/3/2014 ║ 2068 ║
║ PQRS ║ Item Decsription 2 ║ 1/5/2014 ║ 412.104 ║
║ PQRS ║ Item Decsription 2 ║ 1/5/2014 ║ 635.448 ║
║ UVWX ║ Item Decsription 3 ║ 1/5/2014 ║ 214.52 ║
║ UVWX ║ Item Decsription 3 ║ 1/7/2014 ║ 827.2 ║
╚════════════╩════════════════════╩══════════╩═════════╝
I want to display this result as:
╔════════════╦════════════════════╦══════════╦══════════╦══════════╦══════════╗
║ UNIQUECODE ║ DESCRIPTION ║ 1/3/2014 ║ 1/5/2014 ║ 1/7/2014 ║ Total ║
╠════════════╬════════════════════╬══════════╬══════════╬══════════╬══════════╣
║ ABCD ║ Item Decsription 1 ║ 2068 ║ 0 ║ 0 ║ 2068 ║
║ PQRS ║ Item Decsription 2 ║ 0 ║ 1047.552 ║ 0 ║ 1047.552 ║
║ UVWX ║ Item Decsription 3 ║ 0 ║ 214.52 ║ 827.2 ║ 1041.72 ║
╚════════════╩════════════════════╩══════════╩══════════╩══════════╩══════════╝
Best Answer
Your best bet is probably to use dynamic SQL to pivot your data. Here try this out.