views:

46

answers:

4

I have two tables

Table FOO 
FooUniqueID| Year| Name| Worth|
---------------------------
1           2008   Bob    23.00 
2           2009   Bob    40200

Table Bar 
BarUniqueID | Name | Value
-----------------------
 1aBc         Year   2009

I would like to create a view. That will select everything from FOO where the Year is equal

select value from Bar where name = year

without using a sub query.

thank you

+1  A: 

It depends on what kind of program is at work. This would work for some SQL flavors, I believe.

select value from FOO, Bar where FOO.year = Bar.year
Smandoli
needs to work in sql server 2008
george9170
+2  A: 
create view baz as 
select f.FooUniqueID, f.Year, f.Name as FooName, f.Worth,
    b.BarUniqueID, b.Name as BarName, b.Value 
from foo f 
inner join bar b on f.Year = b.Value and b.name = 'Year'
RedFilter
+1  A: 

I don't think there's much point creating a VIEW for this alone, it's a trivial join:

SELECT FOO.*
FROM Bar
JOIN FOO ON FOO.Year=Bar.Value
WHERE Bar.Name='Year';
bobince
The view contains much more data, but I limited the problem to the underlying issue. I need to create a view that grabs a config value from another table.
george9170
+1, that is still trivial, watch out for nesting too many views, performance will go down
KM
+1  A: 
SELECT 
  FooUniqueID, Year, Name, Worth
FROM
  FOO
JOIN
  BAR on FOO.Year = BAR.Value
WHERE
  BAR.Name = 'Year' 
James Westgate