简体   繁体   English

Mysql-默认将行计数为定义的值并将其分组

[英]Mysql - Count rows up to a defined value by default and group them

A little help: I have to count some numbers up to a defined value and group them by id. 有一点帮助:我必须计算一些数字直到定义的值,然后按ID将它们分组。 For example: 例如:
my query: 我的查询:

SELECT part_number, 
NAME, 
NAME2,
count(id) as tot_prod, 
min(serial_number) as serie_min, 
max(serial_number) as serie_max, 
opt_db.Quant as qty_default
FROM my_db, opt_db  
where my_db.NAME2 = '17EM_2'  and  my_db.name2=opt_db.vs 
group by my_db.number order by my_db.SERIAL_NUMBER

my_db result: my_db结果:

+-------------+-------+-------+----------+-----------+-----------+---------+
|   number    | NAME  | NAME2 | tot_prod | serie_min | serie_max | default |
+-------------+-------+-------+----------+-----------+-----------+---------+
| 312705      | 17E21 | 7EM_2 |        3 | 21895     | 21897     |       10|
| 311971      | 17E21 | 7EM_2 |       20 | 21900     | 21920     |       10|
| 311972      | 17E21 | 7EM_2 |        6 | 21925     | 21930     |       10|
+-------------+-------+-------+----------+-----------+-----------+---------+

But I want this OUTPUT: 但是我想要这个输出:

+-------------+-------+-------+-----+-------+----------+----------+--------+
|   number    | NAME  | NAME2 | tot | PACK  |serie_min |serie_max |default |
+-------------+-------+-------+-----+-------+----------+----------+--------+
| 312705      | 17E21 | 7EM_2 |   3 |  3    |  21895   | 21897    |   10   |
| 311971      | 17E21 | 7EM_2 |  20 | 10    |  21900   | 21910    |   10   |
| 311971      | 17E21 | 7EM_2 |  20 | 10    |  21911   | 21920    |   10   |
| 311972      | 17E21 | 7EM_2 |   6 |  6    |  21925   | 21930    |   10   |
+-------------+-------+-------+-----+-------+----------+----------+--------+

SOLVED: 解决了:

SELECT GROUP_CONCAT(distinct m.SERIAL_NUMBER order by m.SERIAL_NUMBER ) as series, 
            m.partnumber,
            m.NAME,
            m.NAME2,
            count(distinct m.id) as tot_prod, 
            min(m.serial_number) as serie_min, 
            max(m.serial_number) as serie_max, 
            e.Quantidade as qty_default,
            e.qty_faltam
            FROM my_db as m,  op_db  as e
            where m.NAME = 'nameExample' and  m.name2=e.name2 and m.partnumber = e.part_num
            group by m.partnumber order by series

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

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