Click here to Skip to main content
15,890,609 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
The following code listed below is used to bring data from the table (weekly_data wd) but it also includes nulls. I need the left join due to needing the other fields irrespective when there is data or not. In the event if there is data how would I exclude null values from weekly data

What I have tried:

SQL
select sm.sugar_mill_name, mbz.mill, fhi.hczname as zone, sd.name as station, 
    round(coalesce(wd.dvalue, 0), 1) as Station_Reading
from sugar_mills sm join
     mill_by_zone mbz
     on sm.sugar_mill_name = mbz.mill join
     fca_hcz_info fhi
     on fhi.hcz = mbz.zone join 
     zone_by_station zbs
     on zbs.zone = fhi.hcz join 
     station_details sd
     on sd.station_num = zbs.station left join 
     weekly_data wd
     on wd.station_num = sd.station_num and
        wd.station_num = zbs.station and 
        wd.record_year = 2019 and
        wd.record_week = 19 and
        wd.dcode = 1;
Posted
Updated 12-May-19 0:15am
v2

LEFT JOIN - by design - will bring NULLs for all the failed joins... So if you are looking up a value in the joined table that does not exists it will not exclude the original row but fill the gaps with NULLs...
Use INNER JOIN - it will eliminate all the original rows, that has no joined rows in the joined table...
http://www.sql-join.com/sql-join-types[^]
 
Share this answer
 
I'm not entirely sure, and I cannot test it right now, but I think on the last (left) join you are introducing elements which seem to belong to a WHERE clause in a JOIN statement.
Maybe try:
SQL
select sm.sugar_mill_name, mbz.mill, fhi.hczname as zone, sd.name as station, 
    round(coalesce(wd.dvalue, 0), 1) as Station_Reading
from sugar_mills sm join
     mill_by_zone mbz
     on sm.sugar_mill_name = mbz.mill join
     fca_hcz_info fhi
     on fhi.hcz = mbz.zone join 
     zone_by_station zbs
     on zbs.zone = fhi.hcz join 
     station_details sd
     on sd.station_num = zbs.station left join 
     weekly_data wd
     on wd.station_num = sd.station_num
where wd.record_year = 2019 and
      wd.record_week = 19 and
      wd.dcode = 1;


Moreover, wd.station_num is already joined to sd.station_num, which itself is joined to zbs.station; there is no need to account for wd.station_num = zbs.station.

Hope this helps.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900