PIVOT sql server (1 Viewer)

CedarTree

Registered User.
Local time
Today, 12:36
Joined
Mar 2, 2018
Messages
404
I have a dynamic number of fields and want to use a pass-through query (Pivot function) to grab pivoted data.

Table structure:
Client Person DataField DataValue
ABC 123 Name John
ABC 123 Age 23
ABC 456 Name Jane
ABC 456 Age 25
XYZ 123 Name John
XYZ 123 Age 23

What I want the query to return (for client ABC only):
Person Name Age
123 John 23
456 Jane 25

Note that the # of datafields (columns) could change.

Struggling with Pivot syntax. Thanks for any help!
 

isladogs

MVP / VIP
Local time
Today, 17:36
Joined
Jan 14, 2017
Messages
18,216
Your original table should have both Name & DOB in the same record.
Age should NEVER be stored as it (obviously) changes.
Instead it should be calculated in a query.

So it sounds like you need to go back & modify your table design rather than mess around with pivot tables (which were deprecated from 2013 onwards)
 

CedarTree

Registered User.
Local time
Today, 12:36
Joined
Mar 2, 2018
Messages
404
Actually I am storing DOB. I was using Age as an example. I'm interested in the Pivot syntax. What replaced Pivot after 2013?
 

Users who are viewing this thread

Top Bottom