即使在使用DISTINCT
之后,我仍然会在[CONMAT_MATCHING_DONOR]上获得重复项。
SELECT TOP 1000 [CONTRIB_MATCH_ID]
,[CONMAT_CONTRIBUTION]
,[CONMAT_FORM_RECEIVED_DATE]
,[CONMAT_MATCHING_DONOR]
,[CONMAT_STATUS]
,[STATUS_DESC]
,[CONMAT_STATUS_DATE]
FROM [ods_production].[dbo].[SPT_CONTRIB_MATCH]
WHERE [CONMAT_MATCHING_DONOR] IN (SELECT DISTINCT
[CONMAT_MATCHING_DONOR]
FROM [ods_production].[dbo].[SPT_CONTRIB_MATCH])
ORDER BY [CONMAT_MATCHING_DONOR] DESC
答案 0 :(得分:1)
您在DISTINCT
子句中对IN
的使用没有多大意义 - 这不会以任何方式影响查询结果。
考虑:
WITH v_base(name) AS (
SELECT 'A' UNION ALL
SELECT 'A' UNION ALL
SELECT 'B')
SELECT name FROM v_base WHERE name IN (SELECT DISTINCT name from v_base)
或多或少转换为
WITH v_base(name) AS (
SELECT 'A' UNION ALL
SELECT 'A' UNION ALL
SELECT 'B')
SELECT name FROM v_base WHERE name IN ('A', 'B')
VS
WITH v_base(name) AS (
SELECT 'A' UNION ALL
SELECT 'A' UNION ALL
SELECT 'B')
SELECT DISTINCT name FROM v_base WHERE name IN (SELECT name from v_base)
转换为
WITH v_base(name) AS (
SELECT 'A' UNION ALL
SELECT 'A' UNION ALL
SELECT 'B')
SELECT DISTINCT name FROM v_base WHERE name IN ('A', 'A', 'B')
并且差异应该变得清晰。