I need a query to Flatten / Pivot the data from TableA ( See Flattened Results). For every PK_PATIENT_LEVELDATA row, there would be a column "Varname" with a value "Value".
Please note: not every PK_PATIENT_LEVELDATA can have 1:N number of VARNAMES/VALUES. Basically trying to create "Dynamic" Columns.
Any help would be appreciated. Thank you in advance
TableA:
PK_PATIENT_LEVELDATA VARNAME VALUE
59 AGE 53
60 AGE 62
61 AGE 65
60 APPTWAIT 5
61 APPTWAIT 10
59 BARCODE 1
60 BARCODE 1
61 BARCODE 1
60 CONTACT No
61 CONTACT No
59 DAYWEEK Saturday-Sunday
60 DAYWEEK MONDAY
61 DAYWEEK WEDNESDAY
59 DISDD 2
60 DISDD 4
61 DISDD 6
59 DISMM 2
60 DISMM 2
61 DISMM 2
_____________________________________________________________________________________________________________________________
Flattened Results:
PK_PATIENT_LEVELDATA AGE APPTWAIT BARCODE CONTACT DAYWEEK DISDD DISMM
59 53 NULL 1 NULL Saturday-Sunday 2 2
60 62 5 1 No MONDAY 4 2
61 65 10 1 No WEDNESDAY 6 2
Please note: not every PK_PATIENT_LEVELDATA can have 1:N number of VARNAMES/VALUES. Basically trying to create "Dynamic" Columns.
Any help would be appreciated. Thank you in advance
TableA:
PK_PATIENT_LEVELDATA VARNAME VALUE
59 AGE 53
60 AGE 62
61 AGE 65
60 APPTWAIT 5
61 APPTWAIT 10
59 BARCODE 1
60 BARCODE 1
61 BARCODE 1
60 CONTACT No
61 CONTACT No
59 DAYWEEK Saturday-Sunday
60 DAYWEEK MONDAY
61 DAYWEEK WEDNESDAY
59 DISDD 2
60 DISDD 4
61 DISDD 6
59 DISMM 2
60 DISMM 2
61 DISMM 2
_____________________________________________________________________________________________________________________________
Flattened Results:
PK_PATIENT_LEVELDATA AGE APPTWAIT BARCODE CONTACT DAYWEEK DISDD DISMM
59 53 NULL 1 NULL Saturday-Sunday 2 2
60 62 5 1 No MONDAY 4 2
61 65 10 1 No WEDNESDAY 6 2