I have a table where multiple rows are needing to be used as columns. Google tells me PIVOT is for just this sort of thing. I start by reading MSDN / Technet. This seems straight forward and mentions...
and performs aggregations where they are required
So I start to write my pivot only their examples all use aggregation functions. So I go back to Google for some other examples that might not use aggregation and every example is using aggregation...so I begin to wonder did I read the definition correctly? I thought aggregation was not REQUIRED just available.
and performs aggregations where they are required....but I don't require any so....?
My table looks like this:
SELECT * FROM [ADHOC_FIELD] where TEMPLATE_ID = 356
CONTACT_ID | FIELD_NAME | TEMPLATE_ID | VALUE | ROW_ID
1017674 Orig_Name 356 John 1
1017674 Orig_EMail 356 @.com 1
1017674 Orig_Trans 356 ABC 1
1017674 Orig_WFID 356 1001 1
I would like the data to present like this:
CONTACT_ID | Orig_Name | Orig_Email | Orig_Trans | Orig_WFID
1017674 John @.com ABC 1001
As I wrote this SO prompted me with several post about pivot without aggregation but they were all more complex than my simple little requirement.
This one seemed like a winner but even then they used row_number and then the aggregate function MAX?
So I'm not asking for anyone to do my work just trying to understand....does PIVOT require aggregation or not?
EDIT
So based upon everyone's feedback I wrote this PRIOR to Zoff Dino answer.
SELECT
[1] AS 'recv_date',
[2] AS 'send_date',
[3] AS 'proc_name',
....etc
FROM
(
SELECT VALUE, ROW_NUMBER() OVER (ORDER BY CONTACT_ID) rn
FROM [ADHOC_FIELD] WHERE TEMPLATE_ID = 356
) src
PIVOT
(
MAX(VALUE)
FOR rn IN ([1],[2],[3],...)
) piv
And it was working but ONLY returning 1 record. However as Zoff Dino pointed out I wasn't quite straight in my thinking and his answer is perfect.