繁体   English   中英

Mysql组通过在计数值之前的列中显示总值,如何停止此操作?

[英]Mysql Group By showing total of values in a column before counted values, how to stop this?

我正在处理查询以在多个列中查找重复的值,因此,我将从关注查询的单个部分开始,以获得更好的解释。

最终,我只需要知道这4列中是否有重复项,以及该重复项所在的列即可。

这是单个查询:

select  count(*) as cnt, 'CUST_REF' as what_column
 from sometable 
  where status != 'whateverStatus' 
    and custm_id = 1234
 group by cust_ref having count(cust_ref) > 1;

所以这很好用,除了输出是2行。 看起来第一行是该列中的总匹配数> 1,然后下一行是实际重复计数,如下所示:

cnt what_column
9440    CUST_REF
2   CUST_REF

我的问题是我怎么能只获得第二行而没有该列的总数呢? (此列的值2是正确的)即我只想要:

cnt what_column    
2   CUST_REF

把它放在一起:

我将所有这些与一个UNION放在一起,因此对于4列,它将是这样的:

select  count(*) as cnt, 'CUST_REF' as what_column
 from sometable 
  where status != 'whateverStatus' 
    and custm_id = 1234
 group by cust_ref having count(cust_ref) > 1
 union
 select  count(*) as cnt, 'CUST_PO' as what_column
 from sometable 
  where status != 'whateverStatus' 
    and custm_id = 1234
 group by cust_po having count(cust_po) > 1
  union
 select count(*) as cnt, 'SHIP_BL' as what_column
 from sometable 
  where status != 'whateverStatus' 
    and custm_id = 1234
 group by ship_bl having count(ship_bl) > 1
  union
 select count(*) as cnt, 'CUST_SHIPID' as what_column
 from sometable 
  where status != 'whateverStatus' 
    and custm_id = 1234
 group by cust_shipid having count(cust_shipid) > 1;

输出的结果如下:我想将所有显示重复项的字段归为一组,同时也忽略了总数。

cnt what_column
9440    CUST_REF
2   CUST_REF
332 CUST_PO
3   CUST_PO
2   CUST_PO
8   CUST_PO
4   CUST_PO
9   CUST_PO
37  CUST_PO
6   CUST_PO
5   CUST_PO
7   CUST_PO
11  CUST_PO
6609    SHIP_BL
2   SHIP_BL
5   SHIP_BL
8   SHIP_BL
3   SHIP_BL
4   SHIP_BL
6   SHIP_BL
7   SHIP_BL
9183    CUST_SHIPID
2   CUST_SHIPID
3   CUST_SHIPID
6   CUST_SHIPID

同样,到最后,我只需要知道这4列中的任何一个都有重复项,以及该重复项所在的列即可。

对于下面的那些评论 ,我无法共享表数据。 但是,在将列添加回HAVING的select之后,让我们这样看:

select cust_ref as val, count(*) as cnt, 'CUST_REF' as what_column
     from sometable 
      where status != 'whateverStatus' 
        and custm_id = 1234
     group by cust_ref having count(cust_ref) > 1;

HAVING中的所有列名称都是该表中的实际列名称, what_column只是一个别名,它向我显示在其中找到重复项的列/查询。

假设数据看起来像这样,我在前两列中用*标记了重复项。 我希望这能使他们大胆:

id | cust_ref | cust_po | ship_bl |cust_shipid
997| **1234** | 9656    | 5656    | 9876
998| **1234** | **6353**| 2436    | 9394
999| 4327     | **6353**| 4388    | 4353

我很确定我最终会得到:

val cnt what_column
      3 CUST_REF
1234  2 CUST_REF

希望有帮助!

您对看起来很简单的问题的解释非常复杂,并且您还没有清楚地解释要计为“重复”的内容-您是否希望对一个值出现多次的总记录进行计数,或者出现不止一次的值的计数?

您通过将重复值的计数与域的计数相混淆进一步混淆了事情-它的巧合是查询输出中的第二行是2-这不是您要查找的值,恰好是相同的基数。

该列的值2是正确的

表明您想要后者。 在这种情况下,因为:

select  cust_ref, count(*) as cnt, 'CUST_REF' as what_column
from sometable 
where status != 'whateverStatus' 
   and custm_id = 1234
group by cust_ref having count(cust_ref) > 1;

将为您提供前者,您只需要计算该查询输出的行数即可。 您可以通过以下两种方式执行此操作:

SELECT COUNT(*) AS number_of_values_in_more_than_row, what_column
FROM (
   select  count(*) as cnt, 'CUST_REF' as what_column, cust_ref
   from sometable 
   where status != 'whateverStatus' 
      and custm_id = 1234
   group by cust_ref 
   having count(cust_ref) > 1
)
GROUP BY what_column

....要么....

select  count(DISTINCT cust_ref) as cnt, 'CUST_REF' as what_column
from sometable 
where status != 'whateverStatus' 
    and custm_id = 1234
group by cust_ref 
having count(DISTINCT cust_ref) > 1;

您已经找到了重复项。 因此,如果只希望没有cnt列的列,请执行子查询:

select distinct what_column 
 from (
select  count(*) as cnt, 'CUST_REF' as what_column
from sometable 
 where status != 'whateverStatus' 
 and custm_id = 1234
group by cust_ref having count(cust_ref) > 1
union
 select  count(*) as cnt, 'CUST_PO' as what_column
 from sometable 
 where status != 'whateverStatus' 
  and custm_id = 1234
 group by cust_po having count(cust_po) > 1
union
 select count(*) as cnt, 'SHIP_BL' as what_column
from sometable 
 where status != 'whateverStatus' 
and custm_id = 1234
 group by ship_bl having count(ship_bl) > 1
union
select count(*) as cnt, 'CUST_SHIPID' as what_column
  from sometable 
where status != 'whateverStatus' 
and custm_id = 1234
group by cust_shipid having count(cust_shipid) > 1);

最终工作的答案是在外部查询上使用了having子句,这返回了需要的正确数字:

SELECT sum(cnt) as dupes, COUNT(*) AS number_of_values_in_more_than_row, what_column
  FROM (
select  count(*) as cnt, 'CUST_REF' as what_column,cust_ref
 from sometable 
  where status != 'whateverStatus' 
    and custm_id = 1234
 group by cust_ref having count(cust_ref) > 1
 union
 select  count(*) as cnt, 'CUST_PO' as what_column,cust_po
 from sometable 
  where status != 'whateverStatus' 
    and custm_id = 1234
 group by cust_po having count(cust_po) > 1
  union
 select count(*) as cnt, 'SHIP_BL' as what_column,ship_bl
 from sometable 
  where status != 'whateverStatus' 
    and custm_id = 1234
 group by ship_bl having count(ship_bl) > 1
  union
 select count(*) as cnt, 'CUST_SHIPID' as what_column,cust_shipid
 from sometable 
  where status != 'whateverStatus' 
    and custm_id = 1234
 group by cust_shipid having count(cust_shipid) > 1
 )x
 GROUP BY what_column having count(number_of_values_in_more_than_row) >0;

暂无
暂无

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

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