简体   繁体   English

如何在一个表中对MYSQL中的一系列行求和,匹配另一个表中的信息以获得结果

[英]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. 这段代码在Access中效果很好。

I have two tables. 我有两张桌子。

Table1 has the columns: CNES, PROCEDIMENTO, MES, ANO, QTDE 表1具有以下列: 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) QTDE =来自医院(CNES)的手术量(PROCEDIMENTO),在一个月(MES)和一年(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 表2具有以下列: 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]) 该表包含一个月内医院需要执行的程序数(META)和该程序的实际名称(NOME。PROCEDIMENTO是一个数字,但我将其存储为字符串[code])

So my query, that works in Access, would do this from that example above 因此,我的查询(可在Access中使用)将从上面的示例中完成此操作

Query1 查询1

+------+--------------+------+-----+-----+------+------------+
| 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). 除此之外,我的查询没有执行此操作,但是我希望这样做...我需要另一列来显示%(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.... 但是我需要使超过100%的%%变为100%...并且我需要小心除以零问题....

I think this works fine in MySQL 5.5.30: 我认为这在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;

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

相关问题 如何使用PHP或MySQL对一个表中的不同列求和,并在另一表中获得求和结果? - How to sum different columns from one table and get the sum result in another table using PHP or MySQL? 有效地从一个表中删除与另一个表不匹配的行 [MySQL] - Efficiently deleting rows from one table where not matching another [MySQL] 从表中选择结果以匹配mysql中的2行 - Select result from table for matching 2 rows in mysql 来自结果表mysql的两行总和 - Sum of two rows from result table mysql MySQL:如何从一个表中选择和显示所有行,并计算另一个表中where子句的总和? - MySQL: How to select and display ALL rows from one table, and calculate the sum of a where clause on another table? MySQL连接:即使secon表没有匹配数据,如何从一个表中获取结果 - MySQL join: How to get result from one table even if secon table has no matching data 根据另一个MySQL表中的信息显示一个表的结果 - Show result from one table depending on info from another MySQL table MySql 从另一个表中为每个 id 获取一个字段的 SUM - MySql Get SUM of one field for every id from another table 从具有2列匹配mysql的3个表中获取结果 - Get result from 3 table with 2 column matching mysql SUM信息的子查询未包含在JOIN MySQL的表结果中 - Subquery to SUM info not included in table result from JOIN MySQL
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM