[英]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.