views:

112

answers:

5

MySQL noob with a table-joining question:

I have two tables, "splits" and "dividends" that describe events in the stock market.

The splits table has "day", "ratio", and "ticker". The dividends table has "day", "amount", and "ticker".

I would like to get a resulting joined table that has both tables' information, AND sorted by date and ticker. So something like this: (sorry about the formatting)

splits.day splits.ratio splits.ticker dividends.day dividends.amount dividends.ticker

1990-01-03      2            QQQQ          null           null            null

null          null            null        1995-05-05      15.55          SPY

2000-09-15     3             DIA           null            null           null

null            null           null        2005-03-15     3             DIA

I looked up full outer joins on wikipedia (using unions on mysql) but I couldn't figure out how to get it to be sorted by the day... Any help would be greatly appreciated!

EDIT: here's an example of what splits and dividends contain in the above example

splits.day splits.ratio splits.ticker

1990-01-03      2            QQQQ

2000-09-15     3             DIA



dividends.day dividends.amount dividends.ticker

1995-05-05      15.55          SPY

2005-03-15     3.55             QQQQ
A: 

to do Full Outer Join :

select splits.day splits.ratio splits.ticker dividends.day dividends.amount dividends.ticker from splits , dividends where splits.ticker = dividends.ticker order by splits.day
Haim Evgi
A: 

I fail to understand why do you need the nulls you use in your example, does this mean not every row in one table has every "ticker" on the other? Is even "ticker" the relation between the two tables?

If all the assumptions above are correct, then something like this should be what you want:

select splits.*, dividends.* from splits left join dividends 
on (splits.ticker = dividends.ticker) 
order by splits.day, dividends.day, splits.ticker

EDIT: Based on the comments, you might be looking for a union

select a.ticker,a.ratio_or_amount,a.day,a.what from (
select ticker, day, ratio as ratio_or_amount, 'splits' as what from splits
union 
select ticker, day, amount as ratio_or_amount, 'dividends' as what from dividends
) a order by a.day

This is ugly, but I'm really failing at what you are after. It looks like you miss a refresher on relational theory.

Vinko Vrsalovic
this query's results are not really what I'm looking for.The two tables really are not directly related at all. For example, the splits table could have three entries with a particular ticker, and its possible for that ticker to be present in the dividends table or absent.
Then what exactly is it you want, a NxN result with all the rows combined between the two tables? a Union?
Vinko Vrsalovic
i'd like to have all the info from splits and dividends merged into one table, ordered by the date. i edited the question to show both the original tables and the results i want.
A: 

You might want to have a look at GREATEST() ( http://dev.mysql.com/doc/refman/5.0/en/comparison-operators.html#function_greatest ) in order to create a comparable date row. Do not, however, that the NULL values might be a problem in MySQL >= 5.0.13.

jensgram
A: 

You shouldn't have to use unions. You should be able to do this (sorry, I don't have MySQL available to test):

select 
    ifnull(splits.day, dividends.day) as day, splits.ratio, 
    ifnull(splits.ticker, dividends.ticker) as ticker, dividends.amount
from splits 
    outer join dividends on 
        splits.day = dividends.day and splits.ticker = dividends.ticker
order by day, ticker
Jacob
i'm getting an sql error with this.
the thing is, the dates and tickers on splits and dividends are not the same. So if there's a particular day/ticker on splits, its almost certain that this day/ticker combo does NOT exist on dividends.
+1  A: 

OK, based on your edits, it looks to me like you don't really want a join at all. You probably want to do this:

select *
from 
(
    select day, ticker, ratio, null as amount
    from splits

    union

    select day, ticker, null as ratio, amount
    from dividends
) as q
order by day, ticker
Jacob
yes! this is pretty much what i want, though i figured it on my own about the same time as your post.for reference, here's my query:SELECT day, ticker, ratios as number, adjusted, "1" AS issplitFROM splits UNIONSELECT day, ticker, amount as number, adjusted, "0" as issplitFROM dividendsORDER BY ticker, day