简体   繁体   中英

How to Sum a series of rows in MYSQL from one Table, matching the info from another to get the result

SELECT table2.*, 
       (SELECT Sum(qtde) 
        FROM   table1 
        WHERE  table1.cnes = table2.cnes 
               AND table1.procedimento = table2.procedimento 
               AND table1.mes = table2.mes 
               AND table1.ano = table2.ano 
        GROUP  BY cnes, 
                  procedimento, 
                  ano, 
                  mes) AS soma 
FROM   table2 

This code works great in Access.

I have two tables.

Table1 has the columns: CNES, PROCEDIMENTO, MES, ANO, QTDE

It may contain information like this (and I can't change this fact, I get the info from another software)

+------+--------------+-----+-----+------+
| CNES | PROCEDIMENTO | MES | ANO | QTDE |
+------+--------------+-----+-----+------+
| C1   | P1           | M1  | A1  |   12 |
| C1   | P1           | M1  | A1  |    3 |
| C1   | P1           | M1  | A1  |    5 |
| C1   | P2           | M1  | A1  |    4 |
| C1   | P2           | M1  | A1  |    4 |
| C1   | P2           | M1  | A1  |    3 |
| C1   | P3           | M1  | A1  |    4 |
| C2   | P2           | M1  | A1  |    5 |
| C2   | P2           | M1  | A1  |    4 |
| C2   | P1           | M1  | A1  |    3 |
+------+--------------+-----+-----+------+

QTDE = quantity of procedures (PROCEDIMENTO), from an hospital (CNES), done in a month (MES) and year (ANO)

So to know the quantity of procedures from one hospital to a single month i need to search all the rows and sum all the rows that's from that month...

Table2 has the columns: CNES, PROCEDIMENTO, NOME, MES, ANO, META

This table contains the number of procedures the hospital needs to perform in a month (META) and the actual name of the procedure (NOME. PROCEDIMENTO is a number but i store it as string[code])

So my query, that works in Access, would do this from that example above

Query1

+------+--------------+------+-----+-----+------+------------+
| CNES | PROCEDIMENTO | NOME | MES | ANO | META | SUM(QTDE)  |
+------+--------------+------+-----+-----+------+------------+
| C1   | P1           | N1   | M1  | A1  |   23 |         20 |
| C1   | P2           | N2   | M1  | A1  |   20 |         11 |
| C1   | P3           | N3   | M1  | A1  |   04 |          4 |
| C2   | P2           | N2   | M1  | A1  |   05 |          9 |
| C2   | P1           | N1   | M1  | A1  |   01 |          3 |
+------+--------------+------+-----+-----+------+------------+

If you take a closer look you will realize that the last column is the sum of quantity for that month so I can use that to see if the hospital did the quantity of procedures it was supposed to do in that month.

Besides that, and my query doesn't do that, but I wish it did... I would need another column to show the % (SUM(QTDE)/META). But I need to make %% that are over 100% to be just 100%... and I need to careful with divisions by zero problems....

I think this works fine in MySQL 5.5.30:

select *, 
 if(meta = 0 or soma > meta, 100, (soma/meta)*100) Percent
from
(
 SELECT table2.*, 
   (SELECT Sum(qtde) 
    FROM   table1 
    WHERE  table1.cnes = table2.cnes 
           AND table1.procedimento = table2.procedimento 
           AND table1.mes = table2.mes 
           AND table1.ano = table2.ano 
    GROUP  BY cnes, 
              procedimento, 
              ano, 
              mes) AS soma
FROM   table2) TMP;

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