Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
| Author |
Topic |
|
ds9
Starting Member
38 Posts |
Posted - 2007-02-16 : 05:45:01
|
| Hi thereHi have a table with some parameters like this (it's imported from xls with the same format):Product|ParameterA|ParameterB|ParameterC XPTO| 5 | 10 | good XYZX| 29 | 22 | goodAnd I have another table with the description of each parameter, like this:Parameter Code | Parameter Name | Parameter description |1 | ParameterA | blábláblá2 | ParameterB | blábláblá3 | ParameterC | blábláblaHow do I link these two tables to show something likeProduct | ParameterA | Parameter descriptionMany thanksDS9 |
|
|
swatib
Posting Yak Master
173 Posts |
Posted - 2007-02-16 : 07:16:31
|
Please be specific. Your column name "ParameterA" in first table is value of a column in second table.Njoy Life |
 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2007-02-16 : 07:38:37
|
what is the relationship of these 2 table ? What is the expected result from the sample data provided ? KH |
 |
|
|
ds9
Starting Member
38 Posts |
Posted - 2007-02-16 : 09:53:23
|
| HiThat's it swatib! The column names in the first table are column values in the second table. And I want to build a view tha basically adds the Paramter description to the parameter value like this:Product | ParameterA | Parameter descriptionXPTO 5 bláblábláthanks ds9 |
 |
|
|
swatib
Posting Yak Master
173 Posts |
Posted - 2007-03-01 : 01:56:47
|
Could you please provide better sample input and output. What is the role of ParametrB and ParameterC?Njoy Life |
 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2007-03-01 : 02:08:24
|
With insufficient description / details on your requirement, poor sample data and unmatched result . . . I think this is what you wantdeclare @atable table( Product varchar(10), ParameterA varchar(10), ParameterB varchar(10), ParameterC varchar(10))insert into @atable select 'XPTO', '5', '10', 'good' union allselect 'XYZX', '29', '22', 'good'declare @anothertable table( [Parameter Code] int, [Parameter Name] varchar(10), [Parameter description] varchar(12))insert into @anothertableselect 1, 'ParameterA', 'blábláblá' union allselect 2, 'ParameterB', 'blábláblá' union allselect 3, 'ParameterC', 'bláblábla' union allselect 4, 'good', 'good blá' union allselect 5, '5', '5 blá' union allselect 6, '10', '10 bláblá'select a.Product, b.[Parameter Name], b.[Parameter description]from @atable a inner join @anothertable b on a.ParameterA = b.[Parameter Name]union allselect a.Product, b.[Parameter Name], b.[Parameter description]from @atable a inner join @anothertable b on a.ParameterB = b.[Parameter Name]union allselect a.Product, b.[Parameter Name], b.[Parameter description]from @atable a inner join @anothertable b on a.ParameterC = b.[Parameter Name]/*Product Parameter Name Parameter description ---------- -------------- --------------------- XPTO 5 5 bláXPTO 10 10 blábláXPTO good good bláXYZX good good blá*/ If this is not what you want, please re-read mine & swatib's post. KH |
 |
|
|
ds9
Starting Member
38 Posts |
Posted - 2007-03-01 : 06:26:43
|
| HiFirst of all thank you for your persistence. It is highly appreciated.Let me try to rephrase my question. I have 2 tables. The only relation between the 2 tables is that the header of one equals the contents of one of the fields in the second table. Here is another (hopefully better) example:TABLE AProduct|AverageWeigth|AverageQty|TotalOutputApple|20|13493|239444Strawberry|5|5000|43443Orange|25|2324|235244TABLE BParameterCode|ParameterName|ParameterDescription1|AverageWeigth|Means the average weight in grams of each unit during one week2|AverageQty|Means the average quantity produced per day during one week3|TotalOutput|Means the total quantity produced during one weekThe output I want is a kind of "copy paste transpose" of table A with some additional info from table B:ParameterName|Apple|Strawberry|Orange|ParameterDescriptionAverageWeigth|20|5|25|Means the average weight in grams of each unit during one weekAverageQty|13493|5000|2324|Means the average quantity produced per day during one weekTotalOutput|239444|43443|235244|Means the total quantity produced during one weekAgainMany thanksds9 |
 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2007-03-01 : 07:11:22
|
[code]declare @A table( Product varchar(10), AverageWeigth int, AverageQty int, TotalOutput int)insert into @Aselect 'Apple', 20, 13493, 239444 union allselect 'Strawberry', 5, 5000, 43443 union allselect 'Orange', 25, 2324, 235244declare @B table( ParameterCode int, ParameterName varchar(15), ParameterDescription varchar(70))insert into @Bselect 1, 'AverageWeigth', 'Means the average weight in grams of each unit during one week' union allselect 2, 'AverageQty', 'Means the average quantity produced per day during one week' union allselect 3, 'TotalOutput', 'Means the total quantity produced during one week'select b.ParameterName, [Apple] = sum(case when a.Product = 'Apple' then case b.ParameterName when 'AverageWeigth' then a.AverageWeigth when 'AverageQty' then a.AverageQty when 'TotalOutput' then a.TotalOutput else 0 end else 0 end ), [Strawberry] = sum(case when a.Product = 'Strawberry' then case b.ParameterName when 'AverageWeigth' then a.AverageWeigth when 'AverageQty' then a.AverageQty when 'TotalOutput' then a.TotalOutput else 0 end else 0 end ), [Orange] = sum(case when a.Product = 'Orange' then case b.ParameterName when 'AverageWeigth' then a.AverageWeigth when 'AverageQty' then a.AverageQty when 'TotalOutput' then a.TotalOutput else 0 end else 0 end ), b.ParameterDescriptionfrom @A a cross join @B bgroup by b.ParameterName, b.ParameterDescriptionorder by b.ParameterName/*ParameterName Apple Strawberry Orange ParameterDescription --------------- ----------- ----------- ----------- ---------------------------------------------------------------------- AverageQty 13493 5000 2324 Means the average quantity produced per day during one weekAverageWeigth 20 5 25 Means the average weight in grams of each unit during one weekTotalOutput 239444 43443 235244 Means the total quantity produced during one week*/[/code] KH |
 |
|
|
|
|
|
|
|