我很難找出正確構建此查詢的正確語法。我試圖顯示來自SalesHistoryDetail和SalesVsBudget表的所有記錄。我相信我的查詢允許SalesVsBudget上的一些記錄不會被拖動,而我希望它們都在這段時間,不管是否有相應的銷售。這裏是我的代碼:如何顯示多個表中的所有記錄,而不考慮連接語句上的匹配
SELECT MAX(a.DispatchCenterOrderKey) AS DispatchCenter,
a.CustomerKey,
CASE WHEN a.CustomerKey IN
(SELECT AddressKey
FROM FinancialData.dbo.DimAddress
WHERE AddressKey >= 99000 AND AddressKey <= 99599) THEN 1 ELSE 0 END AS InterCompanyFlag,
MAX(a.Customer) AS Customer,
a.SalesmanID,
MAX(a.Salesman) AS Salesman,
a.SubCategoryKey,
MAX(a.SubCategoryDesc) AS Subcategory,
SUM(a.Value) AS SalesAmt,
b.FiscalYear AS Year,
b.FiscalWeekOfYear AS Week,
MAX(c.BudgetLbs) AS BudgetLbs,
MAX(c.BudgetDollars) AS BudgetDollars
FROM dbo.SalesHistoryDetail AS a
LEFT OUTER JOIN dbo.M_DateDim AS b ON a.InvoiceDate = b.Date
FULL OUTER JOIN dbo.SalesVsBudget AS c ON a.SalesmanID = c.SalesRepKey
AND a.CustomerKey = c.CustomerKey
AND a.SubCategoryKey = c.SubCategoryKey
AND b.FiscalYear = c.Year AND b.FiscalWeekOfYear = c.WeekNo
GROUP BY a.SalesmanID, a.CustomerKey, a.SubCategoryKey, b.FiscalYear, b.FiscalWeekOfYear
有跡象表明,我是從,顯然SalesHistoryDetail表和SalesVsBudget表拉兩個不同的數據集。我希望從SalesVsBudget表中獲得所有budgetLbs和BudgetDollars值,而不管它們是否在連接中匹配。我想要所有匹配的連接記錄,但我也想從SalesVsBudget獲取每條記錄。基本上我想顯示所有的銷售記錄,並且我想在推銷員,客戶,子類別,年份和周匹配時參考SalesVsBudget的預算值,但是我也希望查看落入我的日期範圍但沒有相應的預算分錄當時的銷售記錄。希望這是有道理的。我覺得我非常接近,但我的預算數字並不能反映整個故事,我認爲這是因爲我的一些記錄被排除在外!請幫忙。