簡體   English   中英

自加入以查找重復項但包括所有列

[英]Self Join to find duplicates but including all columns

我想匹配日志表中的任何條目,這些條目發生在同daycause不止一次出現在表中。 我編寫的查詢已經獲取了重復項,我的問題是我需要訪問表中結果中的所有列以供以后的 JOIN 使用。 表看起來像這樣:

| ID | DATE       | CAUSE | USER | ... |
|--------------------------------------|
| x  | 2017-01-01 | aaa   | 100  | ... |
| x  | 2017-01-02 | aaa   | 101  | ... |
| x  | 2017-01-03 | bbb   | 101  | ... |
| x  | 2017-01-03 | bbb   | 101  | ... |
| x  | 2017-01-04 | ccc   | 101  | ... |
| x  | 2017-01-04 | ccc   | 101  | ... |
| x  | 2017-01-04 | ccc   | 101  | ... |
| x  | 2017-01-05 | aaa   | 101  | ... |
| .....................................|
| .....................................|
| .....................................|

詢問:

SELECT logs.* FROM 
    (SELECT day, cause FROM logs 
         GROUP BY day, cause HAVING COUNT(*) > 1) AS logsTwice, logs 
WHERE logsTwice.day = logs.day AND logsTwice.cause = logs.cause

子選擇獲取完全正確的數據(日期和原因),但是當我嘗試獲取這些匹配項的附加列時,我得到了完全錯誤的數據。 我究竟做錯了什么?

嘗試這個:

SELECT logs.* FROM logs
inner join 
(SELECT day, cause FROM logs GROUP BY day, cause HAVING COUNT(*) > 1) logsTwice
on logsTwice.day = logs.day AND logsTwice.cause = logs.cause

您可以只使用窗口函數:

SELECT l.*
FROM (SELECT l.*,
             COUNT(*) OVER (PARTITION BY day, cause) as cnt
      FROM logs l
     ) l
WHERE cnt > 1;

通常,窗口函數將比使用JOINGROUP BY的等效查詢具有更好的性能。

你可以試試

SELECT l1.*
  FROM logs l1
 INNER JOIN logs l2
    ON (l1.id <> l2.id
        AND l1.day = l2.day
        AND l1.cause = l2.cause
        AND l1.user <> l2.user);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM