MySQL- MAX COUNT多个组

时间:2016-11-22 13:06:32

标签: mysql

我有下表:

╒═══════════╤════════════╤════════════╕
│ iddmp2g_2 │ A_BCF_name │ A_BTS_name │
╞═══════════╪════════════╪════════════╡
│ 1         │ CAR.Inem   │ CAR.Inem_A │
├───────────┼────────────┼────────────┤
│ 2         │ CAR.Inem   │ CAR.Inem_A │
├───────────┼────────────┼────────────┤
│ 3         │ CAR.Inem   │ CAR.Inem_A │
├───────────┼────────────┼────────────┤
│ 4         │ CAR.Inem   │ CAR.Inem_A │
├───────────┼────────────┼────────────┤
│ 5         │ CAR.Inem   │ CAR.Inem_B │
├───────────┼────────────┼────────────┤
│ 6         │ CAR.Inem   │ CAR.Inem_B │
├───────────┼────────────┼────────────┤
│ 7         │ CAR.Inem   │ CAR.Inem_B │
├───────────┼────────────┼────────────┤
│ 8         │ CAR.Inem   │ CAR.Inem_1 │
├───────────┼────────────┼────────────┤
│ 9         │ CAR.Inem   │ CAR.Inem_1 │
├───────────┼────────────┼────────────┤
│ 10        │ CAR.Inem   │ CAR.Inem_1 │
├───────────┼────────────┼────────────┤
│ 11        │ CAR.Inem   │ CAR.Inem_2 │
├───────────┼────────────┼────────────┤
│ 12        │ CAR.Inem   │ CAR.Inem_2 │
└───────────┴────────────┴────────────┘

如果我将以下查询应用到表中,我得到:

SELECT A_BCF_name, COUNT(A_BTS_name) from dmp2g_2
group by A_BCF_name, A_BTS_name

结果:

╔════════════╤════════════╤═══════════════════╗
║ A_BCF_name │ A_BTS_name │ COUNT(A_BTS_name) ║
╠════════════╪════════════╪═══════════════════╣
║ CAR.Inem   │ CAR.Inem_A │ 4                 ║
╟────────────┼────────────┼───────────────────╢
║ CAR.Inem   │ CAR.Inem_B │ 3                 ║
╟────────────┼────────────┼───────────────────╢
║ CAR.Inem   │ CAR.Inem_1 │ 3                 ║
╟────────────┼────────────┼───────────────────╢
║ CAR.Inem   │ CAR.Inem_2 │ 2                 ║
╚════════════╧════════════╧═══════════════════╝

下一个方法是根据最后一个字符(数字或字母)得到两种A_BTS_name组,如下所示:

SELECT A_BCF_name, A_BTS_name, COUNT( A_BTS_name ) 
FROM dmp2g_2
WHERE SUBSTRING_INDEX(A_BTS_name,'_',-1) REGEXP '[0-9]+' /*getting only records that end in a number*/
GROUP BY A_BCF_name, A_BTS_name

输出:

╔════════════╤════════════╤═══════════════════╗
║ A_BCF_name │ A_BTS_name │ COUNT(A_BTS_name) ║
╠════════════╪════════════╪═══════════════════╣
║ CAR.Inem   │ CAR.Inem_1 │ 3                 ║
╟────────────┼────────────┼───────────────────╢
║ CAR.Inem   │ CAR.Inem_2 │ 2                 ║
╚════════════╧════════════╧═══════════════════╝

获取以字母或字母结尾的记录。

SELECT A_BCF_name, A_BTS_name, COUNT( A_BTS_name ) 
FROM dmp2g_2
WHERE SUBSTRING_INDEX(A_BTS_name,'_',-1) REGEXP '[A-Za-z]' /*filter records that ends in a letter*/
GROUP BY A_BCF_name, A_BTS_name

输出:

╔════════════╤════════════╤═══════════════════╗
║ A_BCF_name │ A_BTS_name │ COUNT(A_BTS_name) ║
╠════════════╪════════════╪═══════════════════╣
║ CAR.Inem   │ CAR.Inem_A │ 4                 ║
╟────────────┼────────────┼───────────────────╢
║ CAR.Inem   │ CAR.Inem_B │ 3                 ║
+────────────┼────────────┼───────────────────+

此时一切正常。

我的问题是如何获得每种群体的最大数量?我需要获得CAR.Inem_A和CAR.Inem_B之间的最大数量以及CAR.Inem_1和CAR.Inem_2之间的最大数量。结果应该是这样的:

╔════════════╤════════════════════════╤════════════════════════╗
║ A_BCF_name │ MAX(A_BTS_name_number) │ MAX(A_BTS_name_letter) ║
╠════════════╪════════════════════════╪════════════════════════╣
║ CAR.Inem   │ 3                      │ 4                      ║
╚════════════╧════════════════════════╧════════════════════════╝

我感谢任何帮助

感谢

1 个答案:

答案 0 :(得分:0)

这是一个查询,为您提供所要求的信息,但结果以更易于查询的方式排列。

select
 t3.A_BCF_name,
 t3.record_type,
 max(t3.record_count) as max_record_count
from
  (
   select
    t2.A_BCF_name,
    t2.A_BTS_name,
    t2.record_type,
   count(t2.record_type) record_count
   from 
   (
    select
     t1.iddmp2g_2,
     t1.A_BCF_name,
     t1.A_BTS_name,
     case when SUBSTRING_INDEX(t1.A_BTS_name,'_',-1) REGEXP '[A-Za-z]' then 'letter' else 'number' end as record_type
    from dmp2g_2 as t1
   ) as t2
   group by t2.A_BCF_name, t2.A_BTS_name, t2.record_type
 ) as t3
group by t3.A_BCF_name, t3.record_type;

这里有2个嵌套查询。最深的嵌套查询只是再次呈现的表,并且动态添加了额外的record_type列。下一个嵌套查询从上面嵌套查询中选择聚合在记录类型上并计算所有组。最后,外部查询汇总在A_BCF_namerecord_type上,并选择每个组的最大数量。

以下是我编写的用于生成示例数据以验证查询是否产生所需结果的代码:

create table dmp2g_2 (
 iddmp2g_2 int unsigned primary key auto_increment,
 A_BCF_name varchar(32) not null,
 A_BTS_name varchar(32) not null,
 index (A_BCF_name),
 index (A_BTS_name)
) engine=innodb;

insert into dmp2g_2 (A_BCF_name, A_BTS_name) values
('CAR.Inem', 'CAR.Inem_A'),
('CAR.Inem', 'CAR.Inem_A'),
('CAR.Inem', 'CAR.Inem_A'),
('CAR.Inem', 'CAR.Inem_A'),
('CAR.Inem', 'CAR.Inem_B'),
('CAR.Inem', 'CAR.Inem_B'),
('CAR.Inem', 'CAR.Inem_B'),
('CAR.Inem', 'CAR.Inem_1'),
('CAR.Inem', 'CAR.Inem_1'),
('CAR.Inem', 'CAR.Inem_1'),
('CAR.Inem', 'CAR.Inem_2'),
('CAR.Inem', 'CAR.Inem_2');

结果:

mysql> select
    ->  t3.A_BCF_name,
    ->  t3.record_type,
    ->  max(t3.record_count) as max_record_count
    -> from
    ->   (
    ->    select
    ->     t2.A_BCF_name,
    ->     t2.A_BTS_name,
    ->     t2.record_type,
    ->    count(t2.record_type) record_count
    ->    from 
    ->    (
    ->     select
    ->      t1.iddmp2g_2,
    ->      t1.A_BCF_name,
    ->      t1.A_BTS_name,
    ->      case when SUBSTRING_INDEX(t1.A_BTS_name,'_',-1) REGEXP '[A-Za-z]' then 'letter' else 'number' end as record_type
    ->     from dmp2g_2 as t1
    ->    ) as t2
    ->    group by t2.A_BCF_name, t2.A_BTS_name, t2.record_type
    ->  ) as t3
    -> group by t3.A_BCF_name, t3.record_type;
+------------+-------------+------------------+
| A_BCF_name | record_type | max_record_count |
+------------+-------------+------------------+
| CAR.Inem   | letter      |                4 |
| CAR.Inem   | number      |                3 |
+------------+-------------+------------------+
2 rows in set (0.00 sec)