Here i wrote a query with left join table shipyard. There is no connection related to this table but its affecting the results when we joined or removed, The question is why ?
Mainly its affecting OrderBook and TotalShips column.
select a.sbwynum,
a.sbnam,
a.deleted,
sum(if ((sh.statuscod = 'O' or sh.statuscod = 'S') and (left (
condeldat, 4) = '2011' or left (adjdeldat, 4) = '2011' or left (
deldat, 4) = '2011'), sh.cgt, 0)) as CurrCgt,
count(if ((sh.statuscod = 'O' or sh.statuscod = 'S') and (left (
condeldat, 4) = '2011' or left (adjdeldat, 4) = '2011' or left (
deldat, 4) = '2011'), 1, NULL)) as CurrShips,
count(if (sh.statuscod = 'O', 1, NULL)) as OrderBook,
count(if (sh.statuscod = 'S', 1, NULL)) as TotalShips,
a.country as coucod,
ct.counam,
a.fulnam,
a.status,
a.stoclist,
if (sh.statuscod = 'O', 1, 2) as StatusFlag
from shipbuilder as a
left join
(select sbwynum, statuscod, condeldat, adjdeldat, deldat, cgt from
`ship` s join shiptype st on s.wytypid = st.wytypid and st.forsearch
= 'Y' and st.searchsb = 'Y' and deleted = 'N') sh on sh.sbwynum =
a.sbwynum
left join country ct on ct.coucod = a.country and ct.deleted = 'N'
left join shipyard sy on a.sbwynum = sy.sbwynum and sy.deleted != 'Y' and
sy.syclsid != 'B'
where a.sbwynum != '' and
a.deleted = 'N' and
a.status != 'FV' and
a.country = '365'
group by a.sbwynum
having a.deleted = 'N'
order by sbnam
Thanks a lot…..
Based on the comment, it means that you have a 1 to many relationship from shipbuilder to shipyard that falls within your join criteria. This should be solved with a select distinct.
Edit The distinct should be applied within your count statements