由于当前一周尚未结束,我当前在数据库中没有数据。在下周的星期一输入数据。因此,该查询未显示任何结果。该查询是使用联接创建的。
select
sm.sugar_mill_name, mbz.mill, fhi.hczname as zone, sd.name as station,
round(nvl(wd.dvalue, 0), 1) as Station_Reading
from
mill_by_zone mbz
join
sugar_mills sm 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
join
weekly_data wd on wd.station_num = sd.station_num and wd.station_num = zbs.station
where
wd.record_year = 2019
and wd.record_week = 19
and wd.dcode = 1
如何显示常量数据,即sm.sugar_mill_name, mbz.mill, fhi.hczname as zone, sd.name as station
round(nvl(wd.dvalue,0),1)
,因为Station_Reading当前为空,因为尚未输入任何数据。
答案 0 :(得分:0)
SQL Server中有几种类型的联接:
看看here这些联接中的每一个意味着什么,它是如何工作的,并了解何时使用某种类型的联接。对于您的具体情况,我认为您需要在每周数据上使用 LEFT JOIN 。
P.S。最佳做法是使用完整的连接名称(INNER JOIN,LEFT JOIN等),而不是仅写术语JOIN。
答案 1 :(得分:0)
您将使用LEFT JOIN
来获取每周数据。这样做时,还需要将列上的过滤条件移至ON
子句:
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;