robthomas09
asked on
SQL - select data in fields as columns
Hello experts,
Now that I have butchered the title of that question, here's my need:
I have two tables, and I a trying to join them for an output, that may involve a pivot.
Table1: jos_rsform_submission_valu es:
[FieldName] [FieldValue]
old_coach Smith
played_football_before Yes
emergency_contact_phone 1237890
emergency_contact Mom
father_cell 1234567
mother_guard_name Jane Doe
Table2: jos_rsform_submission_colu mns
[ColumnName]
old_coach
played_football_before
emergency_contact_phone
emergency_contact
father_cell
mother_guard_name
I would like to end up with:
old_coach played_football_before emergency_contact_phone emergency_contact father_cell mother_guard_name
Smith Yes 1237890 Mom 1234567 Jane Doe
Smith Yes 1237890 Mom 1234567 Jane Doe
Smith Yes 1237890 Mom 1234567 Jane Doe
Smith Yes 1237890 Mom 1234567 Jane Doe
(The above arent dupes, there are many rows in jos_rsform_submission_valu es etc.)
Something like:
select
*
from
(
select P.[FieldName], P.[FieldValue]
from jos_rsform_submission_valu es as P
left outer join jos_rsform_submission_colu mns as PD on PD.[ColumnName] = P.[FieldValue]
) as P
pivot
(
min(P.[FieldName])
for P.[FieldValue] in ([old_coach], [played_football_before], [emergency_contact_phone], [emergency_contact], [father_cell], [mother_guard_name])
) as PIV
Thanks!
Now that I have butchered the title of that question, here's my need:
I have two tables, and I a trying to join them for an output, that may involve a pivot.
Table1: jos_rsform_submission_valu
[FieldName] [FieldValue]
old_coach Smith
played_football_before Yes
emergency_contact_phone 1237890
emergency_contact Mom
father_cell 1234567
mother_guard_name Jane Doe
Table2: jos_rsform_submission_colu
[ColumnName]
old_coach
played_football_before
emergency_contact_phone
emergency_contact
father_cell
mother_guard_name
I would like to end up with:
old_coach played_football_before emergency_contact_phone emergency_contact father_cell mother_guard_name
Smith Yes 1237890 Mom 1234567 Jane Doe
Smith Yes 1237890 Mom 1234567 Jane Doe
Smith Yes 1237890 Mom 1234567 Jane Doe
Smith Yes 1237890 Mom 1234567 Jane Doe
(The above arent dupes, there are many rows in jos_rsform_submission_valu
Something like:
select
*
from
(
select P.[FieldName], P.[FieldValue]
from jos_rsform_submission_valu
left outer join jos_rsform_submission_colu
) as P
pivot
(
min(P.[FieldName])
for P.[FieldValue] in ([old_coach], [played_football_before], [emergency_contact_phone],
) as PIV
Thanks!
ASKER
Thanks for the reply Ste5an, here are the create structures for the two tables above:
CREATE TABLE [dbo].[jos_rsform_submissi on_columns ](
[ColumnName] [varchar](255) NOT NULL
)
CREATE TABLE [dbo].[jos_rsform_submissi on_values] (
[FieldName] [varchar](max) NOT NULL,
[FieldValue] [varchar](max) NOT NULL
)
The part im struggling with is getting the values table converted, writing that insert statement you did above for the sample data is essentially what I need to figure out how to script I believe.
Thanks
CREATE TABLE [dbo].[jos_rsform_submissi
[ColumnName] [varchar](255) NOT NULL
)
CREATE TABLE [dbo].[jos_rsform_submissi
[FieldName] [varchar](max) NOT NULL,
[FieldValue] [varchar](max) NOT NULL
)
The part im struggling with is getting the values table converted, writing that insert statement you did above for the sample data is essentially what I need to figure out how to script I believe.
Thanks
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks!
Open in new window