tags:

views:

53

answers:

2

I have table one which looks like this. And I want to get data like

1.2, "My name is " , 1.6, "Earl" ,12345, "Rock Hard Awesome"

I don't think it is possible with this schema but wanted to see if I am wrong. I tried a cross join but got ever possibility not just the actual values.

What is supposed to be accomplished is the values table is written a lot of times (for a ton of values) but the description table has the unique values that are static and by putting them in a separate table they do not get written over and over.

Values Table
Column names = DataID1|DataID1Value|DataID2|DataID2Value|DataID3|DataID3Value
Row values   = 1        1.2          2       1.6         3        12345

Description Table
Column names = DescriptionID1|Description
Row value   = 1               "My name is"
Row value   = 2               "Earl"
Row value   = 3               "Rock Hard Awesome"
+3  A: 

How about this:

SELECT DISTINCT v.DataID1Value, d1.Description, 
                v.DataID2Value, d2.Description, 
                v.DataID3Value, d3.Description
  FROM Values v
  INNER JOIN Description d1 on d1.DescriptionID = v.DataID1
  INNER JOIN Description d2 on d2.DescriptionID = v.DataID2
  INNER JOIN Description d3 on d3.DescriptionID = v.DataID3
Justin Ethier
I think you missed what part of what I wrote and maybe helped me prove why I am thinking this is not possible.Note you have Desc2 and Desc3. But those don't exist in my current schema.Please correct/forgive me if I am wrong.
Maestro1024
Replace Desc1, Desc2,Desc3 to Desc and run the query
Bharat
Appart from the missing Decsription tablename, this select gives you the output with the given inputs you've provided. If this is not what you need, you should update your question.
Lieven
@Bharat - Thanks, I just fixed the query.
Justin Ethier
@Maestro: Can you be more specific? What exactly did I miss?
Justin Ethier
I sorta get the updated version. However I don't understand the v. and d1. or d2. What are these, variables in the sql? I am using MS sql and when I go to query this I get "The multi-part identifier "v.DataID1Value" could not be bound"
Maestro1024
Yes, basically when you say `FROM table a` or `JOIN table a` you are using the alias `a` to refer to that table. It is a short-hand and also lets you join to the same table multiple times. I am not sure why `v.DataID1Value` would be a problem - this is the same name as the column in your example.
Justin Ethier
I get it now and that appears to work. I think my error with the multi-part thing was a typo of some sort. Much thanks.
Maestro1024
You're welcome, glad to help!
Justin Ethier
A: 

Personally I wouldn't store data that way. What you have is essentially a lookup table that is associated with many columns insted of just one. So you will have to join to it for every column that you want to see the description for. This is possible but will lead to horrible performance problmes as this table will become a locking nightmare.

If you have static values that don't change, look them up (use them for a pull down if you want on your forms) and store them inthe values table on insert. Then you can query only one table.

Then go and read about EAV tables and what a problem they are and why they are poor techinique in database design.

And this is how you query the current table. Note that you join to it mulitple times to get the information you need for each value. The d1, d2, d3 are the differnt aliases you need when you have multiple joins to the same table.

  SELECT DISTINCT v.DataID1Value, d1.Description,  
                v.DataID2Value, d2.Description,  
                v.DataID3Value, d3.Description 
  FROM Values v 
  INNER JOIN Description d1 on d1.DescriptionID = v.DataID1 
  INNER JOIN Description d2 on d2.DescriptionID = v.DataID2 
  INNER JOIN Description d3 on d3.DescriptionID = v.DataID3 
HLGEM
This is the same query I wrote :)
Justin Ethier
NOt quite, yours was wrong becasue it aliased the tables twice.
HLGEM