运行T-SQL查询以连接3个表,dimStores,fctSales,dimDates时遇到问题。
我需要保留dimStores(所选商店列表)和dimDates(日期选择)以及fctSales匹配详细信息的所有行。我只能通过具有商店和日期密钥的fctSales表加入它们。
我尝试了左外连接以获得所选商店的完整列表,然后右外连接以获得所有日期但从未能同时获得这两个。
dimStores(StoreName) fctSales(StoreName,DateKey) dimDates(日期)
SELECT
st.StoreName
,sa.SaleItem
,dd.DateKey
From dimStores as st
Left outer join fctSales as sa on st.StoreName = sa.StoreName
right outer join dimDates as dd on dd.DateKey = sa.DateKey
WHERE dd.DateKey between 20150101 and 20150115
and sa.StoreName in ('Aka','Bink','Gil')
请帮忙
答案 0 :(得分:0)
要保持每个可能日期的所有商店全外连接,以获得所有组合,然后才加入数据。
SELECT
st.StoreName
,sa.SaleItem
,dd.DateKey
From dimStores as st
full outer join dimDates as dd on
1=1
Left outer join fctSales as sa on
st.StoreName = sa.StoreName and
dd.DateKey = sa.DateKey
WHERE
dd.DateKey between 20150101 and 20150115 and
sa.StoreName in ('Aka','Bink','Gil')