January 14, 2010 at 3:05 pm
Hello,
I am trying to pivot the following:
RowId DataElementId NumberValue DateValue
1 1 23
1 2 123
1 3 1/12/2010
2 1 12
2 2 9879
2 3 1/10/2010
Into this:
RowId QtyShipped QtyOrdered AuditDate
1 23 123 1/12/2010
2 12 9879 1/10/2010
Can this be done within a single pivot task?
Thanks,
Padre
January 14, 2010 at 4:53 pm
Something like this? (Please note how I set up the sample date to be ready to use for others...)
Even though SQL Server has several ways of getting your requested results, i doesn't mean that the table structure you're using would be called "best practise". I strongly recommend normalizing your data!
DECLARE @t TABLE (RowId INT ,DataElementId INT , NumberValue INT ,DateValue datetime)
INSERT INTO @t
SELECT 1, 1, 23,null UNION ALL
SELECT 1, 2, 123,null UNION ALL
SELECT 1, 3,null, '1/12/2010' UNION ALL
SELECT 2, 1, 12,null UNION ALL
SELECT 2, 2, 9879,null UNION ALL
SELECT 2, 3,null, '1/10/2010'
SELECT
rowid,
max(CASE WHEN DataElementId=1 THEN NumberValue ELSE NULL END ) AS QtyShipped,
max(CASE WHEN DataElementId=2 THEN NumberValue ELSE NULL END ) AS QtyOrdered,
max(CASE WHEN DataElementId=3 THEN DateValue ELSE NULL END ) AS AuditDate
FROM @t
GROUP BY rowid
January 15, 2010 at 7:22 am
The data I provided was just an example to make the point. I am very familiar with the T-SQL solution suggested and use it frequently. 🙂 I have to get this working in SSIS though. I know that I can get this done chaining together a number of transforms but I was hoping that the Pivot transform could save me some work.
Thanks,
Padre
January 15, 2010 at 8:03 pm
padre (1/14/2010)
Hello,I am trying to pivot the following:
RowId DataElementId NumberValue DateValue
1 1 23
1 2 123
1 3 1/12/2010
2 1 12
2 2 9879
2 3 1/10/2010
Into this:
RowId QtyShipped QtyOrdered AuditDate
1 23 123 1/12/2010
2 12 9879 1/10/2010
Can this be done within a single pivot task?
Thanks,
Padre
I know you already have an answer and I also know that you're probably not the one that designed that mess. Any chance of you talking to the folks that provide the data in such a terrible fashion and get them to normalize it a bit? That would be the way to go.
--Jeff Moden
Change is inevitable... Change for the better is not.
Viewing 5 posts - 1 through 4 (of 4 total)
You must be logged in to reply to this topic. Login to reply