展开联接以不限制数据

时间:2019-01-29 09:40:03

标签: sql join

我有一个奇怪的问题-我知道Joins根据'ON'规定返回匹配的数据,但是我面临的问题是我需要两个表的Business日期都可以,但是同时我需要加入日期以使总数正确

请参见以下代码:

Select 
o.Resort,
o.Business_Date,
Occupied,
Comps,
House,
ADR,
Room_Revenue,
Occupied-(Comps+House) AS DandT,
Coalesce(gd.Projected_Occ1,0) AS Projected_Occ1,
Occupied-(Comps+House)+Coalesce(gd.Projected_Occ1,0) as Total
from Occupancy o
left join Group_Details_HF gd
on o.Business_Date = gd.Business_Date
and o.Resort = gd.resort
UNION ALL
select 
o.Resort,
o.Business_Date,
Occupied,
Comps,
House,
ADR,
Room_Revenue,
Occupied-(Comps+House) AS DandT,
Coalesce(gd.Projected_Occ1,0) AS Projected_Occ1,
Coalesce(Occupied-(Comps+House),0)+Coalesce(gd.Projected_Occ1,0) as Total
from Occupancy_Forecast o
FULL OUTER JOIN Group_Details_HF gd
on o.Business_Date = gd.Business_Date
and o.Resort = gd.resort

当前,这为我提供了“入住率和入住率”预测表中的理想结果,但是,当营业时间在入住率预测表中不存在时,它会忽略group_details表,我需要将两个日期中都存在的日期进行组合的结果或在没有匹配项的情况下为每个结果提供唯一的结果

1 个答案:

答案 0 :(得分:0)

我决定创建另一个数据透视表,存储来自Group_Details_HF的详细信息,然后将这两个表合并在一起,这给了我所需的结果,而不是摆弄连接:)