我有以下(功能齐全)select
使用多个子查询,而我想切换到联接。
SELECT c.SuperGroupNm, c.CostCenterNbr
FROM vw_dimCostCenter c
WHERE c.CostCenterStatusTxt = 'Active' AND CAST(c.CostCenterNbr AS VARCHAR(10)) IN (
SELECT w.WorkerCostCenterCd
FROM vw_dimworker w
WHERE w.WorkerStatusCd IN ('A', 'L') AND w.WorkerKey = (
SELECT o.OrganizationHierarchyManagerWorkerKey
FROM vw_DimOrganizationHierarchy o
WHERE LOWER(o.OrganizationHierarchyUnitLevelFourNm) = LOWER('XYZ')
AND o.OrganizationHierarchyUnitTreeLevelNbr = 4
)
)
我尝试这样编写它,但是当我运行它时我没有得到任何结果,所以很明显我把它弄糊涂了。我在这做错了什么?
SELECT c.SuperGroupNm, c.CostCenterNbr
FROM vw_dimCostCenter c
INNER JOIN vw_DimWorker w ON w.WorkerCostCenterCd = CAST(c.CostCenterNbr AS VARCHAR(10))
INNER JOIN vw_DimOrganizationHierarchy o ON o.OrganizationHierarchyUnitCd = w.WorkerOrganizationUnitCd
WHERE w.WorkerKey = o.OrganizationHierarchyManagerWorkerKey
AND w.WorkerStatusCd IN ('A', 'L')
AND c.CostCenterStatusTxt = 'Active'
AND LOWER(o.OrganizationHierarchyUnitLevelFourNm) = LOWER('XYZ')
AND o.OrganizationHierarchyUnitTreeLevelNbr = 4
答案 0 :(得分:2)
您可以执行以下操作以提高可读性:
SELECT c.SuperGroupNm, c.CostCenterNbr
FROM vw_dimCostCenter c
INNER JOIN vw_dimworker w on CAST(c.CostCenterNbr AS VARCHAR(10)) = w.WorkerCostCenterCd
and w.WorkerStatusCd IN ('A', 'L')
INNER JOIN vw_DimOrganizationHierarchy o on w.WorkerKey = o.OrganizationHierarchyManagerWorkerKey
and LOWER(o.OrganizationHierarchyUnitLevelFourNm) = LOWER('XYZ')
and o.OrganizationHierarchyUnitTreeLevelNbr = 4
WHERE c.CostCenterStatusTxt = 'Active'
答案 1 :(得分:1)
你可以简单地在JOIN(subelect)中移动子选择并将你应用于ON子句,例如:
SELECT c.SuperGroupNm, c.CostCenterNbr
FROM vw_dimCostCenter c
INNER JOIN (
SELECT w.WorkerCostCenterCd
FROM vw_dimworker w
WHERE w.WorkerStatusCd IN ('A', 'L') AND w.WorkerKey = (
SELECT o.OrganizationHierarchyManagerWorkerKey
FROM vw_DimOrganizationHierarchy o
WHERE LOWER(o.OrganizationHierarchyUnitLevelFourNm) = LOWER('XYZ')
AND o.OrganizationHierarchyUnitTreeLevelNbr = 4
) T ON CAST(c.CostCenterNbr AS VARCHAR(10)) = T.WorkerCostCenterCd
and c.CostCenterStatusTxt = 'Active'
或更深的
SELECT c.SuperGroupNm, c.CostCenterNbr
FROM vw_dimCostCenter c
INNER JOIN (
SELECT w.WorkerCostCenterCd
FROM vw_dimworker w
INNER JOIN vw_DimOrganizationHierarchy o ON w.WorkerStatusCd IN ('A', 'L')
AND w.WorkerKey = o.OrganizationHierarchyManagerWorkerKey
AND LOWER(o.OrganizationHierarchyUnitLevelFourNm) = LOWER('XYZ')
AND o.OrganizationHierarchyUnitTreeLevelNbr = 4
) T ON CAST(c.CostCenterNbr AS VARCHAR(10)) = T.WorkerCostCenterCd and c.CostCenterStatusTxt = 'Active'
或更深刻
SELECT c.SuperGroupNm, c.CostCenterNbr
FROM vw_dimCostCenter c
INNER JOIN vw_dimworker w on CAST(c.CostCenterNbr AS VARCHAR(10)) = T.WorkerCostCenterCd
and c.CostCenterStatusTxt = 'Active'
INNER JOIN vw_DimOrganizationHierarchy o ON w.WorkerStatusCd IN ('A', 'L')
AND w.WorkerKey = o.OrganizationHierarchyManagerWorkerKey
AND LOWER(o.OrganizationHierarchyUnitLevelFourNm) = LOWER('XYZ')
AND o.OrganizationHierarchyUnitTreeLevelNbr = 4
答案 2 :(得分:1)
您可以像下面一样重新编写SQL查询。
SELECT c.SuperGroupNm, c.CostCenterNbr
FROM vw_dimCostCenter c
INNER JOIN vw_dimworker W ON w.WorkerCostCenterCd = CAST(c.CostCenterNbr AS VARCHAR(10)) AND w.WorkerStatusCd IN ('A', 'L')
WHERE
w.WorkerKey = (
SELECT o.OrganizationHierarchyManagerWorkerKey
FROM vw_DimOrganizationHierarchy o
WHERE LOWER(o.OrganizationHierarchyUnitLevelFourNm) = LOWER('XYZ')
AND o.OrganizationHierarchyUnitTreeLevelNbr = 4
)
)
或使用vw_DimOrganizationHierarchy
的内部联接SELECT c.SuperGroupNm, c.CostCenterNbr
FROM vw_dimCostCenter c
INNER JOIN vw_dimworker W ON w.WorkerCostCenterCd = CAST(c.CostCenterNbr AS VARCHAR(10)) AND w.WorkerStatusCd IN ('A', 'L')
INNER JOIN vw_DimOrganizationHierarchy O ON W.WorkerKey = O.OrganizationHierarchyManagerWorkerKey AND
LOWER(o.OrganizationHierarchyUnitLevelFourNm) = LOWER('XYZ')
AND o.OrganizationHierarchyUnitTreeLevelNbr = 4
答案 3 :(得分:0)
这应该有用。
SELECT c.SuperGroupNm, c.CostCenterNbr
FROM vw_dimCostCenter c
INNER JOIN vw_dimworker w
ON CAST(c.CostCenterNbr AS VARCHAR(10)) = w.WorkerCostCenterCd
AND c.CostCenterStatusTxt = 'Active'
AND w.WorkerStatusCd IN ('A', 'L')
INNER JOIN vw_DimOrganizationHierarchy o
ON LOWER(o.OrganizationHierarchyUnitLevelFourNm) = LOWER('XYZ')
AND o.OrganizationHierarchyUnitTreeLevelNbr = 4
AND o.OrganizationHierarchyManagerWorkerKey = w.WorkerKey