有关其修订号的条件COUNTing记录集

时间:2019-02-19 11:16:16

标签: mysql count case

以下SQL查询表示患者文档系统中的情况:

mysql> SELECT op.ID, op.patID, op.OP1OPVerfahren, op.revision
    -> FROM dat_optherapie op
    -> WHERE 1 = 1
    -> AND op.OP1OPVerfahren > 0
    -> LIMIT 25;
+----+-------+----------------+----------+
| ID | patID | OP1OPVerfahren | revision |
+----+-------+----------------+----------+
| 59 |   649 |              1 |       33 |
| 60 |   649 |              1 |       34 |
| 61 |   649 |              1 |       35 |
| 62 |   649 |              1 |       36 |
| 63 |   649 |              1 |       37 |
| 64 |   649 |              1 |       38 |
| 65 |   649 |              1 |       39 |
| 66 |   649 |              1 |       40 |
| 67 |   649 |              1 |       41 |
| 68 |   649 |              2 |       42 |
| 69 |   649 |              2 |       43 |
| 70 |   649 |              2 |       44 |
| 71 |   649 |              2 |       45 |
| 72 |   649 |              2 |       46 |
| 73 |   649 |              2 |       47 |
| 74 |   649 |              2 |       48 |
| 75 |   649 |              2 |       49 |
| 76 |   649 |              2 |       50 |
| 77 |   649 |              2 |       51 |
| 78 |   649 |              2 |       52 |
| 79 |   649 |              2 |       53 |
| 80 |   649 |              2 |       54 |
| 81 |   649 |              2 |       55 |
| 82 |   649 |              2 |       56 |
| 83 |   649 |              2 |       57 |
+----+-------+----------------+----------+

解释:相同的patID被赋予了两种不同的数字外科手术程序(OP1OPVerfahren)-数据的每次更改都收到了适当的修订号(修订)。这意味着记录患者数据的人在修改患者数据记录集的过程中犯了一个错误,并将错误的手术程序编号从“ 1”更改为“ 2”。

我可以通过检查导致结果产生的SQL查询中的最高修订来过滤这些数据

+----+-------+----------------+----------+
| ID | patID | OP1OPVerfahren | revision |
+----+-------+----------------+----------+
| 83 |   649 |              2 |       57 |
+----+-------+----------------+----------+

这里没有问题。但是,我希望数据库中存在所有最大修订版OP1OPVerfahren的计数列表。到目前为止的代码片段如下所示:

SELECT COUNT(IF(op.OP1OPVerfahren = '0',1,NULL)) 'Keine Operation durchgeführt',
       COUNT(IF(op.OP1OPVerfahren = '1',1,NULL)) 'Bioenterics Intragastric Ballon (BIB)',
       COUNT(IF(op.OP1OPVerfahren = '2',1,NULL)) 'Gastric Banding',
       COUNT(IF(op.OP1OPVerfahren = '3',1,NULL)) 'Roux-en-Y Gastric Bypass',
       COUNT(IF(op.OP1OPVerfahren = '4',1,NULL)) 'Roux-en-Y Gastric Bypass banded',
       COUNT(IF(op.OP1OPVerfahren = '5',1,NULL)) 'Scopinaro',
       COUNT(IF(op.OP1OPVerfahren = '6',1,NULL)) 'Duodenal Switch (DS)',
       COUNT(IF(op.OP1OPVerfahren = '7',1,NULL)) 'Sleeve Resection',
       COUNT(IF(op.OP1OPVerfahren = '8',1,NULL)) 'Gastric Pacemaker',
       COUNT(IF(op.OP1OPVerfahren = '9',1,NULL)) 'Billroth II',
       COUNT(IF(op.OP1OPVerfahren = '10',1,NULL)) 'Gastroplasty',
       COUNT(IF(op.OP1OPVerfahren = '11',1,NULL)) 'Fobi / Capella Bypass',
       COUNT(IF(op.OP1OPVerfahren = '12',1,NULL)) 'Larrad',
       COUNT(IF(op.OP1OPVerfahren = '13',1,NULL)) 'Santora',
       COUNT(IF(op.OP1OPVerfahren = '14',1,NULL)) 'DJB',
       COUNT(IF(op.OP1OPVerfahren = '15',1,NULL)) 'TOGA',
       COUNT(IF(op.OP1OPVerfahren = '16',1,NULL)) 'Endobarrier',
       COUNT(IF(op.OP1OPVerfahren = '17',1,NULL)) 'Gastric Plication',
       COUNT(IF(op.OP1OPVerfahren = '18',1,NULL)) 'Stomaphyx',
       COUNT(IF(op.OP1OPVerfahren = '19',1,NULL)) 'Omega Loop Bypass',
       COUNT(IF(op.OP1OPVerfahren = '20',1,NULL)) 'Omega Loop Bypass banded',
       COUNT(IF(op.OP1OPVerfahren = '21',1,NULL)) 'Long Limb Bypass',
       COUNT(IF(op.OP1OPVerfahren = '22',1,NULL)) 'Distal Very Long Gastric Bypass (Thurnheer)',
       COUNT(IF(op.OP1OPVerfahren = '23',1,NULL)) 'Endoscopic Sclerosation',
       COUNT(IF(op.OP1OPVerfahren = '24',1,NULL)) 'Swedish Adjustable Gastric Bypass (SAGB)',
       COUNT(IF(op.OP1OPVerfahren = '25',1,NULL)) 'Vertical Banded Gastroplasty (VBG)',
       COUNT(IF(op.OP1OPVerfahren = '26',1,NULL)) 'Plastic Abdominal Wall Reconstruction (PAWR)',
       COUNT(IF(op.OP1OPVerfahren = '27',1,NULL)) 'Inner Hernia Repair',
       COUNT(IF(op.OP1OPVerfahren = '28',1,NULL)) 'Single Anastomosis Duodeno-Ileal Bypass with Sleeve Gastrectomy (SADI-S)',
       COUNT(IF(op.OP1OPVerfahren = '99',1,NULL)) 'Anderes OP-Verfahren'
FROM dat_optherapie op
WHERE op.OP1Datum BETWEEN "1950-01-01" AND "2050-12-31"
AND op.OP1OPVerfahren > 0
-- AND NOT EXISTS (SELECT 1 FROM dat_optherapie op2 WHERE op2.revision > op.revision)
-- AND op.revision = (SELECT max(op2.revision) FROM dat_optherapie op2 WHERE op2.ID = op.ID AND op2.revision > op.revision)
-- GROUP BY op.OP1OPVerfahren;
-- GROUP BY 1;

“-...”表示到目前为止我已经尝试过的选项。

不考虑修订号的两个AND条件,我得到所有OP1OPVerfahren的计数,而不考虑最高修订号。

如何将最高修订版本号作为过滤器集成到该SQL查询中,以便仅计算最后记录的OP1OPVerfahren?

0 个答案:

没有答案