This is your query:
SELECT c.code,c.name, a.ltp as begning, b.ltp as enddate, d.interim_cash,d.interim_rec_date,
CAST(((b.ltp - a.ltp) / a.ltp * 100) AS DECIMAL(10, 2)) as chng
FROM eod_stock a JOIN
eod_stock b
ON a.company_id = b.company_id LEFT OUTER JOIN
company AS c
ON c.ID = a.company_id RIGHT JOIN
divident_info d
ON c.ID = d.company_id
WHERE a.entry_date = "2012-09-24" AND
b.entry_date = "2012-09-25" AND
d.interim_rec_date BETWEEN "2012-09-24" AND "2012-09-25" AND
a.company_id IN (13, 2) AND
d.company_id IN (13, 2);
Personally, I find it very hard to follow queries where left join
s are mixed with right join
s: much easier to have a structure only using left join
s so you can readily see which table drives the query.
In any case, your where
clause is undoing the effects of the outer joins. In addition to the conditions you have explicitly listed, you are also saying:
a.entry_date is not null and
b.entry_date is not null and
d.interim_rec_date is not null and
a.company_id is not null and
d.company_id is not null
It is really hard to say which of these conditions you need to eliminate to get the results you want. What I can say is that some of these should go into the on
clauses rather than the where
clause. That is the best solution. You can also change the where
to be something like this:
WHERE a.entry_date = "2012-09-24" AND
b.entry_date = "2012-09-25" AND
d.interim_rec_date BETWEEN "2012-09-24" AND "2012-09-25" AND
a.company_id IN (13, 2) AND
d.company_id IN (13, 2) AND
(a.entry_date is null or a.company_id is null or b.entry_date is null)
However, you might want only one or two of those conditions. Note the conditions on d
should stay in the where
clause because the right outer join
is keeping all rows in d
, so the would not be NULL
(unless NULL
in the original data).