简体   繁体   中英

Table data count max val

I have two tables in MySQL as

+---------+-----------+
| machine | status    |
+---------+-----------+
| 40001   | Completed | 
| 40001   | Completed | 
| 40001   | Completed | 
| 40001   | Completed | 
| 40001   | Pending   | 
| 40001   | Pending   | 
| 40001   | Pending   | 
| 40001   | Pending   | 
| 40001   | Pending   | 
| 40001   | Pending   | 
+---------+-----------+
And the other one as
+---------+---------+
| machine | packets | 
+---------+---------+
| 40001   |   527   |
| 40001   |   1497  |
| 40002   |   1414  | 
| 40002   |   2796  |
| 40003   |  392    | 
| 40003   |  1663   | 
| 40004   |   500   | 
| 40004   |   1277  |
+-------+----------+

I want to write a select query which gives me the machine, completion count, pending count and max of packets for that machine. So I tried

SELECT machine,max(packets) AS sync,
      sum(if(laststatus='completed', 1, 0)) AS generation,
      sum(if(laststatus != 'completed', 1, 0)) AS pending
FROM machine_status
right join machine_packets on machine_packets.machine=machine_status.machine
GROUP BY machine

But I got :

+---------+------+------------+---------+
| machine | sync | generation | pending |
+---------+------+------------+---------+
| 40001   | 1497 |          8 |       2 | 
| 40002   | 2796 |          4 |       2 | 
| 40003   | 1663 |          6 |       0 | 
| 40004   | 1277 |          0 |       2 | 
| 40005   | 2755 |          0 |       0 | 
| 40006   |  927 |          0 |       0 | 
| 40007   |  306 |          0 |       0 | 
+---------+------+------------+---------+

As we can see the values are doubled in generation and pending column. Where did I go wrong ?

SELECT machine,sync,
      sum(if(laststatus='completed', 1, 0)) AS generation,
      sum(if(laststatus != 'completed', 1, 0)) AS pending
FROM machine_status
right join (select machine, 
                   max(packets) AS sync 
            from machine_packets 
            group by machine) mp on mp.machine=machine_status.machine
GROUP BY machine

They are doubled because machine_packets has 2 records for each id. To avoid this you can move it in a subquery

A safe way to do this is to use union all :

select machine, sum(status = 'completed') then generation,
       sum(status <> 'completed') then pending,
       max(packets) as packets
from ((select machine, status, 0 as packets
       from machine_status
      ) union all
      (select machine, 0, packets
       from machine_packets
      )
     ) m
group by machine;

This will include rows for all machines in either table.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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