0

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

Philip Daubmeier
  • 14,584
  • 5
  • 41
  • 77

4 Answers4

1

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
  • 6,919
  • 3
  • 49
  • 83
1
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'
D'Arcy Rittich
  • 167,292
  • 40
  • 290
  • 283
1

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
  • 528,062
  • 107
  • 651
  • 834
  • 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. –  Apr 19 '10 at 16:53
  • +1, that is still trivial, watch out for nesting too many views, performance will go down – KM. Apr 19 '10 at 17:32
0
SELECT 
  FooUniqueID, Year, Name, Worth
FROM
  FOO
JOIN
  BAR on FOO.Year = BAR.Value
WHERE
  BAR.Name = 'Year' 
James Westgate
  • 11,306
  • 8
  • 61
  • 68