views:

75

answers:

2

In my database, I have 3 tables that have 2 similar columns, Year and Month. These tables aren't linked by anything.

What i want to do is select the distinct year and months from these tables. So where table 1 contains:

2009 MAY (multiple times) 2008 NOVEMBER (multiple times) 2007 MAY (multiple times)

and table 2 and 3 contains:

2009 NOVEMBER (multiple times) 2009 MAY (multiple times) 2008 NOVEMBER (multiple times) 2008 MAY (multiple times)

I want to be able to do a select where it brings back a complete list of years and months from 2009 November to 2007 MAY.

I am struggling to work out the query.

Cheers

+4  A: 
SELECT month, year
  FROM table1
UNION
SELECT month, year
  FROM table2

UNION will automatically return only distinct rows.

Justin Cave
A: 
select distinct year, month from table 1
union
select distinct year, month from table 2
order by year, month

The only problem with this is because your month is the alpha representation its not going to sort properly, but im sure oracle has a function to turn the string representation of a month into a numeric, and you can just sort by that instead.

Ryan Guill
Thanks alot. should of guessed it'd be a union really. For what i'm doing i can use an order by on month. However, if someone were using all months of the year then they can do a decode
Jarede