开发者

overcoming "at least once" condition in a LEFT JOIN

开发者 https://www.devze.com 2023-01-25 06:48 出处:网络
I have the following two tables quote(date,market,ticker,close) and scenario(date1,date2,period,market,ticker,ratio). I have written the following LEFT JOIN query which does what i want except for one

I have the following two tables quote(date,market,ticker,close) and scenario(date1,date2,period,market,ticker,ratio). I have written the following LEFT JOIN query which does what i want except for one problem :

SELECT  S.date1 AS date1, 
        S.date2 AS date2,
        S.period AS period,
        Q.market AS market,
        Q.ticker AS ticker,
        Q.close * EXP(S.ratio) AS scenario

FROM portfolio.quote Q  LEFT JOIN  portfolio.scenario S 

      ON Q.market = S.market AND
         Q.ticker = S.ticker AND
         S.date1 >= '2010-06-01' AND
         S.date2 <= '2010-07-01' AND
         S.period = 'WEEK'

WHERE Q.date = '2010-07-01' 
ORDER BY S.date1, S.date2, S.period, Q.market, Q.ticker

The previous query returns the results and whenever (market,ticker) in the quote table is not matched by an equivalent (market,ticker) in the scenario table a row is added with NULL values for the columns belonging to the scenario table as such :

date1, date2, period, market , ticker, scenario
NULL , NULL , NULL  , 'sp500', 'QEP' , NULL

For the period between '2010-06-01' and '2010-07-01' with period 'WEEK' there may have been many unmatched rows for ('sp500','QEP') but only one row represent this fact with NULL values in the columns belonging to scenario table. Let alone, Within these results there are many unmatched rows for (market,ticker) but there exis开发者_如何转开发ts at least one match so the query doesn't return NULL values for these rows.

What i want to do is for example if Q.date = '2010-07-01' returns 500 rows for quote table, then there would be 500 * (DISTINCT date1,date,period). with NULL values in the scenario column only when the following condition is unmatched:

Q.market = S.market AND
Q.ticker = S.ticker

So for example the NULL results would look something like that :

date1        , date2        , period  , market , ticker, scenario
'2010-06-01' , '2010-06-08' , 'WEEK'  , 'sp500', 'QEP' , NULL
'2010-06-08' , '2010-06-15' , 'WEEK'  , 'sp500', 'QEP' , NULL
'2010-06-15' , '2010-06-23' , 'WEEK'  , 'sp500', 'A'   , NULL

Another example if for example (market,ticker,close) rows that matched Q.date = '2010-07-01' from quote table are:

('sp','A',1)
('sp','AA',2)

And the (date1,date2,period,market,ticker,ratio) rows that matched the conditions

     S.date1 >= '2010-06-01' AND
     S.date2 <= '2010-07-01' AND
     S.period = 'WEEK'

are:

('2010-06-01','2010-06-08','WEEK','sp','A',0.43)
('2010-06-01','2010-06-08','WEEK','sp','AA',0.21)

('2010-06-08','2010-06-20','WEEK','sp','A',0.49)

('2010-06-20','2010-06-27','WEEK','sp','A',0.46)
('2010-06-20','2010-06-27','WEEK','sp','B',0.23)

Then the result of my query should return

('2010-06-01','2010-06-08','WEEK','sp','A',1*EXP(0.43))
('2010-06-01','2010-06-08','WEEK','sp','AA',2*EXP(0.21))

('2010-06-08','2010-06-20','WEEK','sp','A',1*EXP(0.49))
('2010-06-08','2010-06-20','WEEK','sp','AA',NULL)

('2010-06-20','2010-06-27','WEEK','sp','A',1*EXP(0.46))
('2010-06-20','2010-06-27','WEEK','sp','AA',NULL)

Is this sort of thing possible in MySQL ?, really appreciate any help :)


Since you are using LEFT join, the FROM table's (quote) rows are displayed even when there is no exact match with other table (scenario) for ON conditions. The columns in other table are simply replaced with NULL.

However, what you have been wanting to do is display the all rows from scenario table rather than from quote table. So I would suggest you to either use RIGHT join or replace the portion of the query as
FROM portfolio.scenario S LEFT JOIN portfolio.quote Q

Hope that solves your problem.

0

精彩评论

暂无评论...
验证码 换一张
取 消