Friday, February 24, 2012

Can someone help me with this?

Hi,
I have two tables one holds data (#temp) and the other one is a reference table(#temp2). I would like to grab date from #temp table with #temp2 table column value as my data column. If you look at my example below, you should see what I mean

--my data table
create table #temp (misc1 varchar(10), misc2 varchar(10) , misc3 varchar(10))
insert into #temp values ('Manager','NA','Texas')
insert into #temp values ('VP','EMA','London')

--my reference table
create table #temp2 (Property varchar(10), Value varchar(10))
insert into #temp2 values ('misc1','Title')
insert into #temp2 values ('misc2','Region')
insert into #temp2 values ('misc3','Location')

--my pseudo query, ofcourse this does not work and this is where I need
--help
select
misc1 as (select value from #temp2 where property='misc1'),
misc2 as (select value from #temp2 where property='misc2'),
misc3 as (select value from #temp2 where property='misc3')
from #temp2

--To get the results like below
Title Region Location
Manager NA Texas
VP EMA London
Thanks to all for helping!! :)You would have to use a technique called a correlated subquery, where the subqueries in your SELECT clause reference the primary key of the outer table in their WHERE clause.
And I have to tell you, this is database design (known as an EAV, or Entity Attribute Value schema) is one of the WORST database designs possible. This is extremely inefficient, so I hope you aren't going to be loading a lot of data into it or expecting good performance.

No comments:

Post a Comment