您好,我无法理解具有多个表的join
。我正在尝试获取每个表的所有数据(计划,预测,理想,实际)。如果同一数据不在vendor..ideal_labor
中,则下面的查询将忽略edw..f_labor_dtl
中的数据。例如,如果在第edw..f_labor_dtl
天没有来自'2019-02-07'
的数据,即使该查询包含vendor..ideal_labor
上的数据,该查询也将从'2019-02-07'
中省略。如果我只有2个表相互连接(使用left/right join
或full outer join
),则此问题很容易解决。在这里有多个表,我将3维表用作左联接(SB_Finance.dbo.d_Job_Detail
,edw..d_date
,edw..d_store
)。意味着在'2019-02-07'
中总会有edw..d_date
天。当我在left join
上edw..d_date
时,在所有临时表上的表现都像inner join
一样如何?
with
fcst as (
select
DOB as business_date,
unit as store_id,
JobCode as job_code,
sum(Hours) as Hours
from vendor..HotSchedulesOptimalsForecast
group by dob,unit,JobCode
),
schd as (
select
DOB as business_date,
unit as store_id,
JobCode as job_code,
sum(ScheduledMinutes/60.0) as Hours
from vendor..HotSchedulesScheduled
group by dob,unit,JobCode
),
ideal as (
select
business_date,
store_id,
job_code,
sum(ideal_hrs) as Hours
from vendor..ideal_labor
group by business_date,store_id,job_code
),
act as (
select
business_date,
store_id,
job_code,
sum(ttl_hrs) as Hours
from edw..f_labor_dtl
group by business_date,store_id,job_code
)
select
d.business_date,
d.f_year,
d.f_period,
d.week_period,
d.week_year,
d.week_ending,
s.store_id,
s.store_desc,
s.rpt_concept,
s.region,
j.job_code,
j.job_name,
j.Cat_OPKPI,
sum(schd.Hours) as Schd_Hrs,
sum(fcst.Hours) as Fcst_Hrs,
sum(act.Hours) as Actl_Hrs,
sum(ideal.Hours) as Ideal_Hrs
from SB_Finance.dbo.d_Job_Detail j
left join fcst on j.job_code = fcst.job_code
left join schd on j.job_code = schd.job_code
left join ideal on j.job_code = ideal.job_code
left join act act on j.job_code = act.job_code
left join edw..d_date d on d.business_date = fcst.business_date and d.business_date = schd.business_date and d.business_date = ideal.business_date and d.business_date = act.business_date
left join edw..d_store s on s.store_id = fcst.store_id and s.store_id = schd.store_id and s.store_id = ideal.store_id and s.store_id = act.store_id
where
is_closed = 0
and f_year = 2019
and Cat_OPKPI in ('Service','Culinary','Support','Janitorial')
group by
d.business_date,d.f_year,d.f_period,d.week_period,d.week_year,d.week_ending,s.store_id,s.store_desc,s.rpt_concept,s.region,j.job_code,j.job_name,j.Cat_OPKPI
答案 0 :(得分:0)
我在玩join
之后找到了答案。我在所有维度表上使用cross join
,然后使用left join
连接其他数据表。
with
fcst as (
select
DOB as business_date,
unit as store_id,
JobCode as job_code,
sum(Hours) as Hours
from vendor..HotSchedulesOptimalsForecast
group by dob,unit,JobCode
),
schd as (
select
DOB as business_date,
unit as store_id,
JobCode as job_code,
sum(ScheduledMinutes/60.0) as Hours
from vendor..HotSchedulesScheduled
group by dob,unit,JobCode
),
ideal as (
select
business_date,
store_id,
job_code,
sum(ideal_hrs) as Hours
from vendor..ideal_labor
group by business_date,store_id,job_code
),
act as (
select
business_date,
store_id,
job_code,
sum(ttl_hrs) as Hours
from edw..f_labor_dtl
group by business_date,store_id,job_code
), summary as (
select
d.business_date,
d.f_year,
d.f_period,
d.week_period,
d.week_year,
d.week_ending,
s.store_id,
s.store_desc,
s.rpt_concept,
s.region,
j.job_code,
j.job_name,
j.Cat_OPKPI,
isnull(sum(schd.Hours),0) as Schd_Hrs,
isnull(sum(fcst.Hours),0) as Fcst_Hrs,
isnull(sum(act.Hours),0) as Actl_Hrs,
isnull(sum(ideal.Hours),0) as Ideal_Hrs
from SB_Finance.dbo.d_Job_Detail j
cross join edw..d_date d
cross join edw..d_store s
left join fcst on j.job_code = fcst.job_code and d.business_date = fcst.business_date and s.store_id = fcst.store_id
left join schd on j.job_code = schd.job_code and d.business_date = schd.business_date and s.store_id = schd.store_id
left join ideal on j.job_code = ideal.job_code and d.business_date = ideal.business_date and s.store_id = ideal.store_id
left join act on j.job_code = act.job_code and d.business_date = act.business_date and s.store_id = act.store_id
where
is_closed = 0
and d.f_year = 2019
and Cat_OPKPI in ('Service','Culinary','Support','Janitorial')
group by
d.business_date,d.f_year,d.f_period,d.week_period,d.week_year,d.week_ending,s.store_id,s.store_desc,s.rpt_concept,s.region,j.job_code,j.job_name,j.Cat_OPKPI