sql - Select multiple values from same column based on 2 columns -


best explained using example. trials , results bellow example.

there 2 tables (in reality have multiple tables)

table: products

id    name ----------- 1    apple 2    orange 3    pear 

table: attributes

id  prod_id   attr_id   value ---------------------------- 1   1       101     20 2   1       102     red 3   1       103     sweet 4   2       101     30 5   2       102     orange 6   2       103     sour 6   3       101     40  7   3       102     green 8   3       103     sweet 

desired output

name   attr_id 101 'price'   attr_id 102 'taste' ------------------------------------------------------ apple        20           sweet orange       30           sour pear         40           sweet 

i have managed sql till have had call 3 tables , combine column values shown above. can't head wrapped around this. appreciated.

you have query each column separately...

select    p.name,   (select value attributes attr_id=101 , a.prod_id=p.id) price,   (select value attributes attr_id=102 , a.prod_id=p.id) taste products p 

... or ... if using mssql 2008 r2 can use pivot: http://msdn.microsoft.com/en-us/library/ms177410(v=sql.105).aspx


Popular posts from this blog

How to calculate SNR of signals in MATLAB? -

c# - Attempting to upload to FTP: System.Net.WebException: System error -

ios - UISlider customization: how to properly add shadow to custom knob image -