0

我有一个这样的数据集

id,  numberid  
1,   45  
1,   45  
1,   41  ->invalid number, should be 45  
2,   60  
2,   60  
2,   60  
3,   71  
3,   71  
3,   72  ->invalid number, should be 71  

每组行 (1,2,3) 也应包含相同的代码编号。第 1 组的编号无效(已标记),第 3 组的编号无效(已标记)。
如何仅返回包含无效数字的组 ID(如 1 和 3)?

4

3 回答 3

1

这将向您显示组和不太受欢迎的 numberid

select 
    your_table.id, numberid
from your_table 
    inner join  (select id from your_table group by id having count(distinct numberid) >1) distinctitems 
        on your_table.id = distinctitems.id
    inner join 
        (
            select id, max(numbercount) as occurrencesofmostpopular
            from
            (
                select id, numberid, count(*) as numbercount
                from your_table 
                group by id, numberid
            ) a
            group by id
        ) mostpopular
        on your_table.id = mostpopular.id

group by your_table.id, your_table.numberid, occurrencesofmostpopular
having (count(*)<occurrencesofmostpopular) or (occurrencesofmostpopular=1)
于 2012-06-15T13:38:49.063 回答
0

这个查询应该可以解决问题:

SELECT id FROM your_table GROUP BY id HAVING COUNT(numberid) > 1;

如果您想知道什么特定值具有 invlid numberid,那么您将不需要复杂的查询,并且您必须定义“无效数字”是什么。无效数字是当前 ID 出现次数最少的数字吗?

于 2012-06-15T08:56:17.683 回答
0

试试这个查询:

SELECT
  g.id,
  COUNT(g.id)
FROM
  (SELECT id, numberid FROM your_table GROUP BY 1, 2) g
GROUP BY
  1
HAVING
  COUNT(g.id) > 1
于 2012-06-15T10:09:17.520 回答