查询:使用具有相同名称但来自不同表的多个列作为 WHERE 子句的条件?
Queries: Using multiple columns with same name but from different tables as conditions for the WHERE clause?
我想使用来自不同表的 SOME_DATE 和 ANOTHER_DATE 列作为我查询的 WHERE
子句条件的一部分。
如果我从下面的查询中删除 AND ATT.[SOME_DATE] <= '02 december 2020' AND ATT.[ANOTHER_DATE] >= '02 december 2020'
,我只能让它工作:
;WITH cs AS
(
SELECT
t.LKJ_UID,
t.ITEM_UID,
t.[I_T_UID],
t.[I_I_CD],
t.[L_DS],
t.[SOME_DATE],
t.[ANOTHER_DATE],
t.[AN_DATED],
t.[LS_E_DATE],
t.[LS_E_UST_UID],
t.[PTU_UID],
t.[FMT_UID],
t.[DG_LT],
t.[DG_A],
t.[OD],
t.[NTH],
t.[BJU],
t.[BJI],
t.[LOK],
t.[JUT],
ATT.[FG_UID],
ATT.[RTU_UID],
ATT.[LKJ_CD],
ATT.[LKJ_NAME],
ATT.[LKJ_TYPE_UID],
ATT.[LKJ_LENGTH],
ATT.[LCA_KIP_UID],
ATT.[CL_WD],
ATT.[LCA_OCC_LC_UID],
ATT.[SJ_K],
ATT.[SJ_J],
ATT.SOME_DATE as SA_SOME_DATE,
ATT.ANOTHER_DATE as SA_ANOTHER_DATE
FROM
CATT AS t
LEFT OUTER JOIN
ATT ON t.LKJ_uid = ATT.LKJ_uid
WHERE
t.[SOME_DATE] <= '02 december 2020'
AND t.[ANOTHER_DATE] >= '02 december 2020'
AND ATT.[SOME_DATE] <= '02 december 2020'
AND ATT.[ANOTHER_DATE] >= '02 december 2020'
ORDER BY t.[ITEM_UID]
OFFSET 24 * 0 ROWS
FETCH NEXT 24 ROWS ONLY
)
SELECT *
FROM cs
ORDER BY
cs.ITEM_UID
只有当我删除最后一个示例的这一部分时它才会起作用,但我需要它遵守所有 4 个条件,而不仅仅是两个:
AND ATT.[SOME_DATE] <= '02 december 2020'
AND ATT.[ANOTHER_DATE] >= '02 december 2020'
感谢您的帮助
编辑:(回应 Shawnt00 的回答)
此查询 return 72k 行,而您建议的更改 return 160 万条记录:
SELECT dbo.CATT.*,
dbo.ATT.*
FROM dbo.CATT
LEFT OUTER JOIN dbo.ATT
ON dbo.CATT.LKJ_uid =
dbo.ATT.LKJ_uid
WHERE ( dbo.CATT.SOME_DATE <=
CONVERT(DATETIME, '02 december 2020'
,
102) )
AND ( dbo.CATT.ANOTHER_DATE >=
CONVERT(DATETIME, '02 december 2020',
102) )
AND ( dbo.ATT.SOME_DATE <=
CONVERT(DATETIME, '02 december 2020',
102) )
AND ( dbo.ATT.ANOTHER_DATE >=
CONVERT(DATETIME, '02 december 2020',
102) )
将条件移动到联接中。从 where
.
中删除
LEFT OUTER JOIN
section_attributes ON t.section_uid = section_attributes.section_uid
AND SECTION_ATTRIBUTES.START_DATE <= '02 december 2020'
AND SECTION_ATTRIBUTES.END_DATE >= '02 december 2020'
我想使用来自不同表的 SOME_DATE 和 ANOTHER_DATE 列作为我查询的 WHERE
子句条件的一部分。
如果我从下面的查询中删除 AND ATT.[SOME_DATE] <= '02 december 2020' AND ATT.[ANOTHER_DATE] >= '02 december 2020'
,我只能让它工作:
;WITH cs AS
(
SELECT
t.LKJ_UID,
t.ITEM_UID,
t.[I_T_UID],
t.[I_I_CD],
t.[L_DS],
t.[SOME_DATE],
t.[ANOTHER_DATE],
t.[AN_DATED],
t.[LS_E_DATE],
t.[LS_E_UST_UID],
t.[PTU_UID],
t.[FMT_UID],
t.[DG_LT],
t.[DG_A],
t.[OD],
t.[NTH],
t.[BJU],
t.[BJI],
t.[LOK],
t.[JUT],
ATT.[FG_UID],
ATT.[RTU_UID],
ATT.[LKJ_CD],
ATT.[LKJ_NAME],
ATT.[LKJ_TYPE_UID],
ATT.[LKJ_LENGTH],
ATT.[LCA_KIP_UID],
ATT.[CL_WD],
ATT.[LCA_OCC_LC_UID],
ATT.[SJ_K],
ATT.[SJ_J],
ATT.SOME_DATE as SA_SOME_DATE,
ATT.ANOTHER_DATE as SA_ANOTHER_DATE
FROM
CATT AS t
LEFT OUTER JOIN
ATT ON t.LKJ_uid = ATT.LKJ_uid
WHERE
t.[SOME_DATE] <= '02 december 2020'
AND t.[ANOTHER_DATE] >= '02 december 2020'
AND ATT.[SOME_DATE] <= '02 december 2020'
AND ATT.[ANOTHER_DATE] >= '02 december 2020'
ORDER BY t.[ITEM_UID]
OFFSET 24 * 0 ROWS
FETCH NEXT 24 ROWS ONLY
)
SELECT *
FROM cs
ORDER BY
cs.ITEM_UID
只有当我删除最后一个示例的这一部分时它才会起作用,但我需要它遵守所有 4 个条件,而不仅仅是两个:
AND ATT.[SOME_DATE] <= '02 december 2020'
AND ATT.[ANOTHER_DATE] >= '02 december 2020'
感谢您的帮助
编辑:(回应 Shawnt00 的回答)
此查询 return 72k 行,而您建议的更改 return 160 万条记录:
SELECT dbo.CATT.*,
dbo.ATT.*
FROM dbo.CATT
LEFT OUTER JOIN dbo.ATT
ON dbo.CATT.LKJ_uid =
dbo.ATT.LKJ_uid
WHERE ( dbo.CATT.SOME_DATE <=
CONVERT(DATETIME, '02 december 2020'
,
102) )
AND ( dbo.CATT.ANOTHER_DATE >=
CONVERT(DATETIME, '02 december 2020',
102) )
AND ( dbo.ATT.SOME_DATE <=
CONVERT(DATETIME, '02 december 2020',
102) )
AND ( dbo.ATT.ANOTHER_DATE >=
CONVERT(DATETIME, '02 december 2020',
102) )
将条件移动到联接中。从 where
.
LEFT OUTER JOIN
section_attributes ON t.section_uid = section_attributes.section_uid
AND SECTION_ATTRIBUTES.START_DATE <= '02 december 2020'
AND SECTION_ATTRIBUTES.END_DATE >= '02 december 2020'