我有一个数据集如下:
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
)
答案 0 :(得分: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 ,部分问题在于您要将null
与null
进行比较,而这是您无法做到的。因此,您可以对列进行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
)