| Author |
Topic |
|
Benku
Starting Member
10 Posts |
Posted - 2012-01-13 : 00:59:33
|
| Hello,i have the following problem.i have 2 tables. 1 fact and 1 with some descriptions called attributes.(table 1)fact Account Attribute1 Attribute2 Value 1000 a b 100 1000 b a 100 1000 a v 100 1000 f f 100 1000 a e 100 1000 f a 100 1000 b d 100(table 2)Attribute ID ItemID Label 1 a Desc1 2 b Desc2 1 b Desc3 1 f Desc4 2 v Desc5 2 a Desc6I want to obtain the following resultAccount A1 A2 Value1000 Desc1 Desc2 1001000 Desc3 Desc6 1001000 Desc1 Desc5 100doing it for 1 Attribut was easySELECT Label As A1 FROM Attribute WHERE ID = '1'but i don't know how to have to AS statement related to the same table but giving out 2 columns..any help appeciatedBenku |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2012-01-13 : 03:17:38
|
just use simple joins to attribute tableSELECT f.Account,a1.Label,a2.Label,f.ValueFROM fact fINNER JOIN ATtribute a1ON a1.ItemID = f.Attribute1 INNER JOIN ATtribute a2ON a2.ItemID = f.Attribute2 ------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
Benku
Starting Member
10 Posts |
Posted - 2012-01-13 : 04:07:08
|
| Thank you Visakh. It works like a dream and i could also very easily extend it. |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2012-01-13 : 04:31:51
|
| wc------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
Benku
Starting Member
10 Posts |
Posted - 2012-01-13 : 12:04:36
|
| Actually i spoke too fast.the result is that each line appears twice or as many time as i am adding attributes.any suggestions? |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2012-01-13 : 12:08:12
|
| one question.how are tables related?i see ItemID repeating in table2.in that case how you want tables to be related?------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
Benku
Starting Member
10 Posts |
Posted - 2012-01-13 : 13:01:08
|
| That is the tricky part i guess.the ID of table 2 represents the column of table 1so 1 for attribute 1, 2 for Attribute 2...the ItemID represents the various element for each Attribute.Unfortunately, those tables are not mine, so that i cannot change them. i am just trying to retrieve their content to present it in a way that is more convenient for me. |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2012-01-13 : 23:58:21
|
| then how do you determine which accounts descriptions each of them represent in table2?------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
Benku
Starting Member
10 Posts |
Posted - 2012-01-14 : 01:23:18
|
| here an exampletake line 1 of table 1attribute1 = a so from table 2, it means id = 1 and itemID = a so Desc1attribute2 = b so from table 2, it means id = 2 and ItemID = b so Desc2 |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2012-01-14 : 01:29:44
|
quote: Originally posted by Benku here an exampletake line 1 of table 1attribute1 = a so from table 2, it means id = 1 and itemID = a so Desc1attribute2 = b so from table 2, it means id = 2 and ItemID = b so Desc2
no you missed my pointthere are multiple records with id =1 and 2 in table2my question is how do you link this to an accountas of now there's nothing in table2 to determine what account its linked tooSo unless you're sure ID,ItemID combination wont repeat in table2 you'll have difficulty matching descriptions to correct account------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
Benku
Starting Member
10 Posts |
Posted - 2012-01-14 : 02:14:31
|
| you are correct, Table 2 is solely to provide information on attributes, accounts are defined in another table.Now the problem is not match the correct description with the account. that works just fine. the problem is that for each line in table 1, i get 2 lines (identical) in my result.3 if 3 attibutes, 4 if 4 attributes. |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2012-01-14 : 02:17:16
|
| [code]SELECT f.Account,a1.Label,a2.Label,f.ValueFROM fact fINNER JOIN ATtribute a1ON a1.ItemID = f.Attribute1 AND a1.ID=1INNER JOIN ATtribute a2ON a2.ItemID = f.Attribute2AND a2.ID =2[/code]------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
Benku
Starting Member
10 Posts |
Posted - 2012-01-14 : 02:59:40
|
| Thank you. that solved the problem :-) |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2012-01-14 : 04:28:46
|
| wc------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
|