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_values:
[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_columns
[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_values etc.)


Something like:

select
    *
from
(
    select P.[FieldName], P.[FieldValue]
    from jos_rsform_submission_values as P
        left outer join jos_rsform_submission_columns  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!
robthomas09Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

ste5anSenior DeveloperCommented:
hmm, what is your table [jos_rsform_submission_columns ] for? Are you looking for a dynamic SQL?

CREATE TABLE #jos_rsform_submission_columns ( ColumnName sysname );
CREATE TABLE #jos_rsform_submission_values
    (
      old_coach NVARCHAR(255) ,
      played_football_before NVARCHAR(255) ,
      emergency_contact_phone NVARCHAR(255) ,
      emergency_contact NVARCHAR(255) ,
      father_cell NVARCHAR(255) ,
      mother_guard_name NVARCHAR(255)
    );

INSERT  INTO #jos_rsform_submission_values
        ( old_coach ,
          played_football_before ,
          emergency_contact_phone ,
          emergency_contact ,
          father_cell ,
          mother_guard_name
        )
VALUES  ( 'Smith' ,
          'Yes' ,
          '1237890' ,
          'Mom' ,
          '1234567' ,
          'Jane Doe'
        );
 
INSERT  INTO #jos_rsform_submission_columns
        ( ColumnName )
VALUES  ( 'old_coach' ),
        ( 'played_football_before' ),
        ( 'emergency_contact_phone' ),
        ( 'emergency_contact' );

DECLARE @Columns NVARCHAR(MAX);
DECLARE @Sql NVARCHAR(MAX);

SET @Columns = '';
SET @Sql = 'SELECT @Columns FROM #jos_rsform_submission_values;';

SELECT  @Columns = @Columns + ', ' + C.ColumnName
FROM    #jos_rsform_submission_columns C;
SET @Columns = STUFF(@Columns, 1, 2, '');
SET @Sql = REPLACE(@Sql, '@Columns', @Columns);

EXECUTE sys.sp_executesql @Sql;

Open in new window

0
robthomas09Author Commented:
Thanks for the reply Ste5an, here are the create structures for the two tables above:

CREATE TABLE [dbo].[jos_rsform_submission_columns](
      [ColumnName] [varchar](255) NOT NULL
)

CREATE TABLE [dbo].[jos_rsform_submission_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
0
ste5anSenior DeveloperCommented:
E.g. fully dynamic:

CREATE TABLE #jos_rsform_submission_values
    (
      EntityID INT NOT NULL ,
      FieldName NVARCHAR(255) NOT NULL ,
      FieldValue NVARCHAR(MAX) NOT NULL
    );


INSERT  INTO #jos_rsform_submission_values
        ( EntityID, FieldName, FieldValue )
VALUES  ( 1, 'old_coach', 'Smith' ),
        ( 1, 'played_football_before', 'Yes' ),
        ( 1, 'emergency_contact_phone', '1237890' ),
        ( 1, 'father_cell', '1234567' ),
        ( 1, 'mother_guard_name', 'Jane Doe' ),
        ( 2, 'old_coach', 'Biggs' ),
        ( 2, 'played_football_before', 'No' ),
        ( 2, 'emergency_contact_phone', '555-123-4456' ),
        ( 2, 'emergency_contact', 'Dad' ) ,
        ( 2, 'father_cell', '123-321-123' );
 
DECLARE @Columns NVARCHAR(MAX);
DECLARE @Sql NVARCHAR(MAX);

SET @Columns = '';
SET @Sql = '
    SELECT  *
    FROM    #jos_rsform_submission_values 
	pivot ( MIN(FieldValue) FOR FieldName IN ( @Columns ) ) P;
';

WITH    ColumnNames
          AS ( SELECT DISTINCT
                        FieldName
               FROM     #jos_rsform_submission_values
             )
    SELECT  @Columns = @Columns + ', ' + QUOTENAME(FieldName)
    FROM    ColumnNames;

SET @Sql = REPLACE(@Sql, '@Columns', STUFF(@Columns, 1, 2, ''));

EXECUTE sys.sp_executesql @Sql;

DROP TABLE #jos_rsform_submission_values;

Open in new window


or using predefined column names:

CREATE TABLE #jos_rsform_submission_columns ( ColumnName sysname );
CREATE TABLE #jos_rsform_submission_values
    (
      EntityID INT NOT NULL ,
      FieldName NVARCHAR(255) NOT NULL ,
      FieldValue NVARCHAR(MAX) NOT NULL
    );


INSERT  INTO #jos_rsform_submission_values
        ( EntityID, FieldName, FieldValue )
VALUES  ( 1, 'old_coach', 'Smith' ),
        ( 1, 'played_football_before', 'Yes' ),
        ( 1, 'emergency_contact_phone', '1237890' ),
        ( 1, 'father_cell', '1234567' ),
        ( 1, 'mother_guard_name', 'Jane Doe' ),
        ( 2, 'old_coach', 'Biggs' ),
        ( 2, 'played_football_before', 'No' ),
        ( 2, 'emergency_contact_phone', '555-123-4456' ),
        ( 2, 'emergency_contact', 'Dad' ) ,
        ( 2, 'father_cell', '123-321-123' );
		
INSERT  INTO #jos_rsform_submission_columns
        ( ColumnName )
VALUES  ( 'old_coach' ),
        ( 'played_football_before' ),
        ( 'emergency_contact_phone' ),
        ( 'emergency_contact' );

DECLARE @Columns NVARCHAR(MAX);
DECLARE @Sql NVARCHAR(MAX);

SET @Columns = '';
SET @Sql = '
    SELECT  *
    FROM    #jos_rsform_submission_values 
	pivot ( MIN(FieldValue) FOR FieldName IN ( @Columns ) ) P;
';

SELECT  @Columns = @Columns + ', ' + QUOTENAME(ColumnName)
FROM    #jos_rsform_submission_columns;

SET @Sql = REPLACE(@Sql, '@Columns', STUFF(@Columns, 1, 2, ''));

EXECUTE sys.sp_executesql @Sql;

DROP TABLE #jos_rsform_submission_columns;
DROP TABLE #jos_rsform_submission_values;

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
robthomas09Author Commented:
Thanks!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Query Syntax

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.