Insert values into table using a lookup table

Hi,

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.
Thanks
Morpheus7Asked:
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.

Jim HornMicrosoft SQL Server Developer, Architect, and AuthorCommented:
No can do Hoss, for two reasons..

1.

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

2.

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.
0
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.
0
Jim HornMicrosoft SQL Server Developer, Architect, and AuthorCommented:
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.
0
Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

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

Thanks
0
Jim HornMicrosoft SQL Server Developer, Architect, and AuthorCommented:
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, 
   CASE 
      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.
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
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
0
Jim HornMicrosoft SQL Server Developer, Architect, and AuthorCommented:
>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.
0
Morpheus7Author Commented:
Thanks again.
0
Jim HornMicrosoft SQL Server Developer, Architect, and AuthorCommented:
Thanks for the grade.  Good luck with your project.  -Jim
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
Microsoft SQL Server 2005

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.