0

我有一个数据集如下:

SELECT '  1234 ' ID,NULL TAG,' AB' CODE FROM DUAL
UNION ALL 
SELECT '453' ID,'GEN' TAG,'AB' CODE FROM DUAL
UNION ALL
SELECT '1234' ID,NULL TAG,' AB' CODE FROM DUAL)

我正在尝试使用以下查询获取重复项。输出应该是第 1 行和第 3 行,但我只得到一行作为 1 或 3 的修剪输出。

SELECT TRIM(ID),TRIM(TAG),TRIM(CODE) FROM 
(SELECT '  1234 ' ID,NULL TAG,' AB' CODE FROM DUAL
UNION ALL 
SELECT '453' ID,'GEN' TAG,'AB' CODE FROM DUAL
UNION ALL
SELECT '1234' ID,NULL TAG,' AB' CODE FROM DUAL)
WHERE (TRIM(ID),TRIM(TAG),TRIM(CODE)) IN 
(
SELECT TRIM(ID),TRIM(TAG),TRIM(CODE) FROM
(SELECT '  1234 ' ID,NULL TAG,' AB' CODE FROM DUAL
UNION ALL 
SELECT '453' ID,'GEN' TAG,'AB' CODE FROM DUAL
UNION ALL
SELECT '1234' ID,NULL TAG,' AB' CODE FROM DUAL)
GROUP BY TRIM(ID),TRIM(TAG),TRIM(CODE)
HAVING COUNT(*) >1
)
4

1 回答 1

1

我刚刚运行了它,它返回了第 1 行和第 3 行:

SELECT TRIM(ID),TRIM(TAG),TRIM(CODE) 
FROM 
(
    SELECT '  1234 ' ID,NULL TAG,' AB' CODE FROM DUAL
    UNION ALL 
    SELECT '453' ID,'GEN' TAG,'AB' CODE FROM DUAL
    UNION ALL
    SELECT '1234' ID,NULL TAG,' AB' CODE FROM DUAL
)
WHERE TRIM(ID) IN 
(
    SELECT TRIM(ID)
    FROM
    (
        SELECT '  1234 ' ID,NULL TAG,' AB' CODE FROM DUAL
        UNION ALL 
        SELECT '453' ID,'GEN' TAG,'AB' CODE FROM DUAL
        UNION ALL
        SELECT '1234' ID,NULL TAG,' AB' CODE FROM DUAL
    )
   GROUP BY TRIM(ID),TRIM(TAG),TRIM(CODE)
   HAVING COUNT(*) >1
)

我将您更改WHERE为仅引用TRIM(ID)而不是所有 3 个值。

编辑 #1,部分问题是你正在比较nullnull不能做的事情。因此,您可以null检查列,如果是null则替换它。我用它包装了null列,nvl(null, 'na')然后它有一个值可以比较:

SELECT TRIM(ID) id,TRIM(TAG) tag,TRIM(CODE) code 
FROM 
(
    SELECT '  1234 ' ID, nvl(null, 'na') TAG,' AB' CODE FROM DUAL
    UNION ALL 
    SELECT '453' ID,'GEN' TAG,'AB' CODE FROM DUAL
    UNION ALL
    SELECT '  1234 ' ID,nvl(null, 'na') TAG,' AB' CODE FROM DUAL
    UNION ALL 
    SELECT '  1234 ' ID,nvl(null, 'na') TAG,' AC' CODE FROM DUAL
)
WHERE (TRIM(ID),TRIM(TAG),TRIM(CODE)) IN 
(
    SELECT TRIM(ID),TRIM(TAG),TRIM(CODE) 
    FROM
    (
        SELECT '  1234 ' ID,nvl(null, 'na') TAG,' AB' CODE FROM DUAL
        UNION ALL 
        SELECT '453' ID,'GEN' TAG,'AB' CODE FROM DUAL
        UNION ALL
        SELECT '  1234 ' ID,nvl(null, 'na') TAG,' AB' CODE FROM DUAL
        UNION ALL 
        SELECT '  1234 ' ID,nvl(null, 'na') TAG,' AC' CODE FROM DUAL
    )
   GROUP BY TRIM(ID), TRIM(CODE), TRIM(TAG)
   HAVING COUNT(*) >1
)

请参阅带有演示的 SQL Fiddle

于 2012-08-22T19:16:05.053 回答