Note that there are some explanatory texts on larger screens.

plurals
  1. POovercoming "at least once" condition in a LEFT JOIN
    primarykey
    data
    text
    <p>I have the following two tables <code>quote(date,market,ticker,close)</code> and <code>scenario(date1,date2,period,market,ticker,ratio)</code>. I have written the following LEFT JOIN query which does what i want except for one problem :</p> <pre><code>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 &gt;= '2010-06-01' AND S.date2 &lt;= '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 </code></pre> <p>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 :</p> <pre><code>date1, date2, period, market , ticker, scenario NULL , NULL , NULL , 'sp500', 'QEP' , NULL </code></pre> <p>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 exists at least one match so the query doesn't return NULL values for these rows.</p> <p>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:</p> <pre><code>Q.market = S.market AND Q.ticker = S.ticker </code></pre> <p>So for example the NULL results would look something like that :</p> <pre><code>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 </code></pre> <p>Another example if for example <code>(market,ticker,close)</code> rows that matched <code>Q.date = '2010-07-01'</code> from quote table are:</p> <pre><code>('sp','A',1) ('sp','AA',2) </code></pre> <p>And the <code>(date1,date2,period,market,ticker,ratio)</code> rows that matched the conditions </p> <pre><code> S.date1 &gt;= '2010-06-01' AND S.date2 &lt;= '2010-07-01' AND S.period = 'WEEK' </code></pre> <p>are:</p> <pre><code>('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) </code></pre> <p>Then the result of my query should return</p> <pre><code>('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) </code></pre> <p>Is this sort of thing possible in MySQL ?, really appreciate any help :)</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload