简体   繁体   English

SQL - 根据 DB2 中的两列选择重复项

[英]SQL - Select duplicates based on two columns in DB2

I am using DB2 and am trying to count duplicate rows in a table called ML_MEASURE.我正在使用 DB2 并试图对名为 ML_MEASURE 的表中的重复行进行计数。 What I define as a duplicate in this table, is a row containing the same DATETIME and TAG_NAME value.我在此表中定义为重复项的是包含相同 DATETIME 和 TAG_NAME 值的行。 So I tried this below:所以我在下面尝试了这个:

SELECT 
    DATETIME, 
    TAG_NAME, 
    COUNT(*) AS DUPLICATES
FROM 
    ML_MEASURE 
GROUP BY DATETIME, TAG_NAME 
HAVING COUNT(*) > 1

The query doesn't fail, but I get an empty result, even though I now for a fact I have at least one duplicate, when I tried this query below I got the result correct for this specific tag_name and datetime:查询没有失败,但我得到一个空结果,尽管我现在至少有一个重复的事实,当我在下面尝试这个查询时,我得到了这个特定 tag_name 和 datetime 的正确结果:

SELECT
    DATETIME,
    TAG_NAME,
    COUNT(*) AS DUPLICATES
FROM
    ML_MEASURE
WHERE
    DATETIME='2018-03-23 15:09:30' AND
    TAG_NAME='HOG.613KU201'
GROUP BY
    DATETIME,
    TAG_NAME.

The result of the second query looked like this:第二个查询的结果如下所示:

 DATETIME               TAG_NAME        DUPLICATES
 ---------------------  ------------    ----------
 2018-03-23 15:09:30.0  HOG.613KU201             3

What am I doing wrong in the first query?我在第一个查询中做错了什么?

* UPDATE * * 更新 *

My table is row organized, not sure if that makes any difference.我的表是按行组织的,不确定这是否有任何区别。

Yes, you should get the same row back on the first query.是的,您应该在第一个查询中得到相同的行。 If you had a NOT ENFORCED TRUSTED Primary Key or Unique constraint on those two columns, then the Optimizer would be within it's rights to trust the constraint and return you no rows.如果您在这两列上有NOT ENFORCED TRUSTED Primary Key 或 Unique 约束,那么优化器将有权信任该约束并且不返回任何行。 However from a quick test, I don't believe it does that for this query.但是,通过快速测试,我认为它不会针对此查询执行此操作。 Do you have any indexes defined on the table?您是否在表上定义了任何索引?

(PS I assume you are not running the query from a shell prompt and redirecting the output to a file of the name 1 ) (PS 我假设您没有从 shell 提示符运行查询并将输出重定向到名称为1的文件)

This worked for me:这对我有用:

SELECT * FROM (
    SELECT DATETIME, TAG_NAME, COUNT(*) AS DUPLICATES
    FROM ML_MEASURE 
    GROUP BY DATETIME, TAG_NAME 
) WHERE DUPLICATES > 1

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM