Insert values into table using a lookup table


I have a source table with the following definition:

ProdGroup (INT), ProdCode( INT), TestDate (DATETIME), Balance (Float), Content (Float),Intake (Float)

The target table is as follows:
ProdGroup (INT), ProdCode( INT), TestDate (DATETIME), TestID(INT), Result(Float)

Lookup table is as follows:
TestName(VARCHAR10), TestID(INT).

The problem is that I would like to add a row in the target table for each of the 3 results, Balance , Content and Intake.

The 3 tests are set with a corresponding id in the look up table.

An example of the desired result is as follows:

ProdGroup   ProdCode TestDate     TestID  Result
12345               890          20 May 15    1          0.98
12345               890          20 May 15    2          1.08
12345               890          20 May 15    3          2.89

Any guidance would be appreciated.
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

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.

Jim HornMicrosoft SQL Server Data DudeCommented:
No can do Hoss, for two reasons..


I don't see a common column between the source table and the lookup table, needed to get lookup.TestID


I don't see a Result column in either source or lookup tables, so where would these numbers come from?
Perhaps if you post some mockup data of both source and lookup tables this might be clearer.
Morpheus7Author Commented:
Hi Jim,

Thanks for responding. The result in the target table comes from  the Balance , Content and Intake columns in the source table. Instead of having one line for the results, it would be one row per result. In this case 3 rows.
Jim HornMicrosoft SQL Server Data DudeCommented:
That answers #2, still need an answer for #1.  It would be better to give us a data mockup of what's in your source and lookup tables, otherwise this is not enough info here to provide an answer.
IT Pros Agree: AI and Machine Learning Key

We’d all like to think our company’s data is well protected, but when you ask IT professionals they admit the data probably is not as safe as it could be.

Morpheus7Author Commented:
Hi Jim,

Apologies, I hope this clarifies it

Source table is
ProdGroup  ProdCode    TestDate     Balance   Content     Intake
12345               890          20 May 15         0.98       1.08          2.89

Look Up table is
TestID         Test Description
     1                      Balance
     2                      Content
     3                      Intake

The results table would be

ProdGroup   ProdCode TestDate     TestID  Result
 12345               890          20 May 15    1          0.98
 12345               890          20 May 15    2          1.08
 12345               890          20 May 15    3          2.89

Jim HornMicrosoft SQL Server Data DudeCommented:
Ok.  Copy-paste the below into your SSMS, execute and verify that it meets your requirements, then modify to fit your situation
CREATE TABLE #source (ProdGroup int, ProdCode int, TestDate date, balance numeric(3,2), content numeric(3,2), intake numeric(3,2)) 

INSERT INTO #source (ProdGroup, ProdCode, TestDate, balance, content, intake) 
VALUES (12345, 890, '20150520', 0.98, 1.08, 2.89) 

CREATE TABLE #lookup (TestID int, TestDescription varchar(10)) 

INSERT INTO #lookup (TestID, TestDescription) 
VALUES (1, 'Balance'), (2, 'Content'), (3, 'Intake') 

SELECT s.ProdGroup, s.ProdCode, s.TestDate, l.TestID, 
      WHEN l.TestID=1 THEN s.Balance
      WHEN l.TestID=2 THEN s.content
      WHEN l.TestID=3 THEN s.Intake END as Result
FROM #source s
   CROSS JOIN #lookup l

Open in new window

Since we're doing a 'insert a row for every row in table lookup' with no ON clause to relate the two tables, that's a CROSS JOIN.  Also the CASE block is needed to determine which value in source to use based on lookup.TestID.

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
Morpheus7Author Commented:
Hi Jim,

That works great. Just one last thing, if there are more TestIDs in the lookup table than in the source table, how do I prevent returning those rows.

As not all tests will be present in the Source table. For example if TestID 3 was not in the source.

Many thanks
Jim HornMicrosoft SQL Server Data DudeCommented:
>if there are more TestIDs in the lookup table than in the source table, how do I prevent returning those rows.
With a CROSS JOIN you can't unless you can create a WHERE clause that filters all but the ones you want.
And since we're asking to insert a # of rows without a JOIN between the two tables, CROSS JOIN is really your only option unless you want to start hard-coding values, which should be avoided.
Morpheus7Author Commented:
Thanks again.
Jim HornMicrosoft SQL Server Data DudeCommented:
Thanks for the grade.  Good luck with your project.  -Jim
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
Microsoft SQL Server 2005

From novice to tech pro — start learning today.