troubleshooting Question

SQL Query with normalized user defined values into a single table

Avatar of psdavis
psdavisFlag for United States of America asked on
Microsoft SQL Server 2008
12 Comments1 Solution311 ViewsLast Modified:
I'm a good enough of a C# programmer to know that there's always at least five ways to do something in SQL, so I'm going to throw this out to the SQL experts instead.

I have a simple 'Submission' table that returns basic information.  [Table 1]

The database can contain additional user defined fields to the submission table via a table called 'AdditionalFields'.  It only contains a key and a 'Mnemonic' field.  [Table 2]

When a record is saved, the user defiined values are stored in the 'SubmissionFields' table.   [Table 3]

The first output has pivoted the values in the AdditionalFields table into column names.  [Table 4]

The final solution merges the columns of Submission table [Table 1] with the data in [Table 4] to create  [Table 5]

I hope the data makes the question clear.  Thanks for your help!
CREATE TABLE [dbo].[Submissions](
	[SubmissionId] [int] IDENTITY(1,1) NOT NULL,
	[PeopleId] [int] NULL,
[Table 1]
SubmissionId  PeopleId   
 ------------  --------
42            212
97            492
CREATE TABLE [dbo].[AdditionalFields](
	[AdditionalFieldId] [int] IDENTITY(1,1) NOT NULL,
	[Mnemonic] [nvarchar](50) NULL
[Table 2]
AdditionalFieldId       Mnemonic
-----------------       ------------
1                       PlaceOfBirth
2                       SocialSecurityNumber
3                       MotherMaidenName
CREATE TABLE [dbo].[SubmissionFields](
	[SubmissionFieldId] [int] IDENTITY(1,1) NOT NULL,
	[SubmissionId] [int] NOT NULL,
	[AdditionalFieldId] [int] NOT NULL,
	[Value] [nvarchar](50) NOT NULL
[Table 3]
SubmissionFieldId    SubmissionId    AdditionalFieldId   Value
-----------------    --------------  -----------------   ---------
1                    42              1                   TX
2                    42              2                   445129321
3                    42              3                   Smith
4                    97              1                   OK
5                    97              3                   Jones
[Table 4]
SubmissionId   PlaceOfBirth    SocialSecurityNumber    MotherMaidenName
------------   -------------   --------------------    ----------------
42             TX              445129321               Smith
97             OK             null                     Jones
[Table 5]
SubmissionId  PeopleId  PlaceOfBirth    SocialSecurityNumber    MotherMaidenName
------------  --------  -------------   --------------------    ----------------
42            212       TX              445129321               Smith
97            492       OK             null                     Jones
Daniel Wilson

Our community of experts have been thoroughly vetted for their expertise and industry experience.

Join our community to see this answer!
Unlock 1 Answer and 12 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 12 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros