Link to home
Start Free TrialLog in
Avatar of Morpheus7
Morpheus7

asked on

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
Avatar of Jim Horn
Jim Horn
Flag of United States of America image

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.
Avatar of Morpheus7
Morpheus7

ASKER

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.
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.
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
ASKER CERTIFIED SOLUTION
Avatar of Jim Horn
Jim Horn
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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
>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.
Thanks again.
Thanks for the grade.  Good luck with your project.  -Jim