2

我正在尝试查找同一天发生的重复条目。我有一个数据库表,它基本上只包含 ID、USERNAME 和 DATE_CREATED。

我需要一个大致执行此操作的选择:

SELECT USERNAME,DATE_CREATED 
FROM THE_TABLE WHERE {more than one USERNAME exists on date TRUNC(DATE_CREATED)}

是否可以在不仅通过 SELECT 创建过程的情况下做到这一点?谢谢你的建议。

4

4 回答 4

6
SELECT  USERNAME, TRUNC(DATE_CREATED)
FROM    THE_TABLE
GROUP   BY
        USERNAME, TRUNC(DATE_CREATED)
HAVING  COUNT(*) > 1;

例子:

SELECT  USERNAME, TRUNC(DATE_CREATED)
FROM    
(
        SELECT  'a' username, sysdate date_created from dual union all
        SELECT  'a' username, sysdate date_created from dual union all
        SELECT  'b' username, sysdate date_created from dual union all
        SELECT  'b' username, sysdate date_created from dual
)
GROUP   BY
        USERNAME, TRUNC(DATE_CREATED)
HAVING  COUNT(*) > 1;
/*
a   2013-06-18 00:00:00
b   2013-06-18 00:00:00
*/

要在输出中获取完整日期,它有点复杂:

SELECT  DISTINCT
        username
,       date_created
FROM    the_table ot
WHERE   EXISTS
        (
            SELECT  1
            FROM    the_table it
            WHERE   TRUNC(ot.date_created) = TRUNC(it.date_created)
            AND     ot.username            = it.username
            GROUP   BY
                    USERNAME, TRUNC(DATE_CREATED)
            HAVING  COUNT(*) > 1
        )
;
/*
a   2013-06-18 12:48:40
b   2013-06-18 12:48:40
*/

必须访问表两次 + 需要 DISTINCT 关键字。是的,性能可能会降低。

于 2013-06-18T10:38:30.823 回答
5

这将在输出中返回完整日期。

SELECT 
      USERNAME
    , DATE_CREATED 
FROM 
(
  SELECT 
       USERNAME
    ,  DATE_CREATED
    ,  COUNT( *) over ( PARTITION by USERNAME, TRUNC( DATE_CREATED, 'DD') ) cnt
  FROM THE_TABLE
)
WHERE cnt > 1
;
于 2013-06-18T10:54:59.597 回答
2
SELECT USERNAME, trunc(DATE_CREATED)
FROM THE_TABLE 
group by Username,TRUNC(DATE_CREATED)
having count(1) > 1
于 2013-06-18T10:40:37.443 回答
0

使用

select USERNAME, DATE_CREATED 
from test
group by USERNAME, DATE_CREATED 
Having COUNT(1) > 1
于 2013-06-18T10:44:28.130 回答