SQL - 添加一个新的 table 加入当所有离开时加入 - 完全初学者
SQL - Add in a new table join when all left join - Total beginner
我需要帮助向我的查询中添加额外的联接。
所有 tables 目前都是从 SO_Header 左外部连接的,但是我还需要一些来自 table 的数据,称为 RANGE,它只链接到 MODEL table.
如何编辑我的脚本以引入范围 table?
我在 Excel 中使用从 Microsoft 查询中获取数据。
从我的脚本中摘录:
FROM dbo.SO_HEADER SO_HEADER LEFT OUTER JOIN
dbo.WEEK_NUMBERS WEEK_NUMBERS
ON SO_HEADER.SOH_Build_Week = WEEK_NUMBERS.WEEK_Week_No AND
SO_HEADER.SOH_Build_Year = WEEK_NUMBERS.WEEK_Year LEFT OUTER JOIN
dbo.MODEL MODEL
ON SO_HEADER.SOH_Model = MODEL.MODEL_Code LEFT OUTER JOIN
dbo.RETAILERS RETAILERS
ON SO_HEADER.SOH_Retailer = RETAILERS.RET_Code LEFT OUTER JOIN
dbo.FACTORY_OPTIONS FACTORY_OPTIONS
ON SO_HEADER.SOH_Upholstery = FACTORY_OPTIONS.FAC_Code LEFT OUTER JOIN
dbo.FINANCE_HOUSES FINANCE_HOUSES
ON SO_HEADER.SOH_Finance = FINANCE_HOUSES.FIN_Code
假设您的范围 table 中有一个名为 RANGE_MODEL_Code 的外键,那么您可以像这样加入模型和范围
您还可以根据您的业务需求更改加入方式table(i-e让加入、内部加入或外部加入)
FROM dbo.SO_HEADER SO_HEADER LEFT OUTER JOIN
dbo.WEEK_NUMBERS WEEK_NUMBERS
ON SO_HEADER.SOH_Build_Week = WEEK_NUMBERS.WEEK_Week_No AND
SO_HEADER.SOH_Build_Year = WEEK_NUMBERS.WEEK_Year LEFT OUTER JOIN
dbo.MODEL MODEL
ON SO_HEADER.SOH_Model = MODEL.MODEL_Code LEFT OUTER JOIN
dbo.RANGE RANGE
ON MODEL.MODEL_Code = RANGE.RANGE_MODEL_Code LEFT OUTER JOIN
dbo.RETAILERS RETAILERS
ON SO_HEADER.SOH_Retailer = RETAILERS.RET_Code LEFT OUTER JOIN
dbo.FACTORY_OPTIONS FACTORY_OPTIONS
ON SO_HEADER.SOH_Upholstery = FACTORY_OPTIONS.FAC_Code LEFT OUTER JOIN
dbo.FINANCE_HOUSES FINANCE_HOUSES
ON SO_HEADER.SOH_Finance = FINANCE_HOUSES.FIN_Code
从 table i-e RANGE_MODEL_Code 和 [ 中更改您的 table 名称和连接键MODEL_Code 根据你的架构
我需要帮助向我的查询中添加额外的联接。
所有 tables 目前都是从 SO_Header 左外部连接的,但是我还需要一些来自 table 的数据,称为 RANGE,它只链接到 MODEL table.
如何编辑我的脚本以引入范围 table?
我在 Excel 中使用从 Microsoft 查询中获取数据。
从我的脚本中摘录:
FROM dbo.SO_HEADER SO_HEADER LEFT OUTER JOIN
dbo.WEEK_NUMBERS WEEK_NUMBERS
ON SO_HEADER.SOH_Build_Week = WEEK_NUMBERS.WEEK_Week_No AND
SO_HEADER.SOH_Build_Year = WEEK_NUMBERS.WEEK_Year LEFT OUTER JOIN
dbo.MODEL MODEL
ON SO_HEADER.SOH_Model = MODEL.MODEL_Code LEFT OUTER JOIN
dbo.RETAILERS RETAILERS
ON SO_HEADER.SOH_Retailer = RETAILERS.RET_Code LEFT OUTER JOIN
dbo.FACTORY_OPTIONS FACTORY_OPTIONS
ON SO_HEADER.SOH_Upholstery = FACTORY_OPTIONS.FAC_Code LEFT OUTER JOIN
dbo.FINANCE_HOUSES FINANCE_HOUSES
ON SO_HEADER.SOH_Finance = FINANCE_HOUSES.FIN_Code
假设您的范围 table 中有一个名为 RANGE_MODEL_Code 的外键,那么您可以像这样加入模型和范围
您还可以根据您的业务需求更改加入方式table(i-e让加入、内部加入或外部加入)
FROM dbo.SO_HEADER SO_HEADER LEFT OUTER JOIN
dbo.WEEK_NUMBERS WEEK_NUMBERS
ON SO_HEADER.SOH_Build_Week = WEEK_NUMBERS.WEEK_Week_No AND
SO_HEADER.SOH_Build_Year = WEEK_NUMBERS.WEEK_Year LEFT OUTER JOIN
dbo.MODEL MODEL
ON SO_HEADER.SOH_Model = MODEL.MODEL_Code LEFT OUTER JOIN
dbo.RANGE RANGE
ON MODEL.MODEL_Code = RANGE.RANGE_MODEL_Code LEFT OUTER JOIN
dbo.RETAILERS RETAILERS
ON SO_HEADER.SOH_Retailer = RETAILERS.RET_Code LEFT OUTER JOIN
dbo.FACTORY_OPTIONS FACTORY_OPTIONS
ON SO_HEADER.SOH_Upholstery = FACTORY_OPTIONS.FAC_Code LEFT OUTER JOIN
dbo.FINANCE_HOUSES FINANCE_HOUSES
ON SO_HEADER.SOH_Finance = FINANCE_HOUSES.FIN_Code
从 table i-e RANGE_MODEL_Code 和 [ 中更改您的 table 名称和连接键MODEL_Code 根据你的架构