Максимум.пересмотр записей данных в условной установке MySQL множественный COUNT (CASE WHEN THEN END) - PullRequest
0 голосов
/ 17 февраля 2019

Рассмотрим следующий пример набора данных:

SELECT ID, patID, OP1Datum, OP1OPVerfahren, revision FROM table LIMIT 50;

+----+-------+------------+----------------+----------+
| ID | patID | OP1Datum   | OP1OPVerfahren | revision |
+----+-------+------------+----------------+----------+
|  1 |   649 | 0000-00-00 |              0 |        0 |
|  2 |   649 | 2010-09-23 |              0 |        1 |
|  3 |   649 | 2010-09-23 |              0 |        2 |
|  4 |   649 | 2010-09-23 |              0 |        3 |
|  5 |   649 | 2010-09-23 |              0 |        4 |
|  6 |   649 | 2010-09-23 |              0 |        5 |
|  7 |   649 | 2010-09-23 |              0 |        6 |
|  8 |   649 | 2010-09-23 |              0 |        7 |
|  9 |   649 | 2010-09-23 |              0 |        8 |
| 10 |   649 | 2010-09-23 |              0 |        9 |
| 12 |   673 | 2010-08-16 |              0 |        0 |
| 13 |   676 | 2010-07-05 |              0 |        0 |
| 14 |   649 | 2010-02-17 |              0 |       10 |
| 15 |   649 | 2010-02-17 |              0 |       11 |
| 16 |   676 | 2010-07-05 |              0 |        1 |
| 17 |   718 | 2010-12-01 |              0 |        0 |
| 18 |   720 | 0000-00-00 |              0 |        0 |
| 19 |   720 | 0000-00-00 |              0 |        1 |
| 20 |   649 | 2010-02-17 |              0 |       12 |
| 21 |   649 | 2010-02-17 |              0 |       13 |
| 22 |   649 | 2010-02-17 |              0 |       14 |
| 23 |   649 | 2010-02-17 |              0 |       15 |
| 24 |   649 | 2010-02-17 |              0 |       16 |
| 25 |   719 | 2008-12-03 |              0 |        0 |
| 26 |   719 | 2008-12-03 |              0 |        1 |
| 27 |   721 | 2008-12-03 |              0 |        1 |
| 28 |   649 | 2010-02-16 |              0 |       17 |
| 29 |   649 | 2010-02-16 |              0 |       18 |
| 30 |   720 | 2011-02-03 |              0 |        2 |
| 31 |   720 | 2011-02-03 |              0 |        3 |
| 32 |   720 | 2011-02-03 |              0 |        4 |
| 33 |   720 | 2011-02-03 |              0 |        5 |
| 34 |   720 | 2011-02-03 |              0 |        6 |
| 35 |   676 | 2010-07-05 |              0 |        2 |
| 36 |   718 | 2010-12-01 |              0 |        1 |
| 37 |   719 | 2008-12-03 |              0 |        2 |
| 38 |   721 | 2008-12-03 |              0 |        2 |
| 39 |   649 | 2010-02-16 |              0 |       19 |
| 40 |   673 | 2010-08-16 |              0 |        1 |
| 41 |   694 | 2011-02-16 |              0 |        0 |
| 42 |   649 | 2010-02-16 |              0 |       20 |
| 43 |   649 | 2010-10-23 |              0 |       21 |
| 44 |   649 | 2010-02-16 |              0 |       22 |
| 45 |   649 | 2010-02-16 |              0 |       23 |
| 46 |   649 | 2010-02-16 |              0 |       24 |
| 47 |   724 | 2011-03-02 |              0 |        0 |
| 48 |   724 | 2011-03-02 |              0 |        1 |
| 49 |   694 | 2011-02-16 |              0 |        1 |
| 50 |   694 | 2011-02-16 |              0 |        2 |
| 51 |   649 | 2010-02-16 |              0 |       25 |
+----+-------+------------+----------------+----------+

patID 649, например, показывает 26 ревизий записи данных, т.е. эта запись была сохранена 26 раз.

Я хочупоказывать только строки с индивидуально наибольшим номером ревизии.Таким образом, желаемый набор данных будет выглядеть так:

+----+-------+------------+----------------+----------+
| ID | patID | OP1Datum   | OP1OPVerfahren | revision |
+----+-------+------------+----------------+----------+
|  1 |   649 | 0000-00-00 |              0 |       25 |
|  2 |   673 | 2010-08-16 |              0 |        1 |
|  3 |   676 | 2010-07-05 |              0 |        2 |
|  4 |   718 | 2010-12-01 |              0 |        1 |
|  5 |   719 | 2008-12-03 |              0 |        2 |
|  6 |   720 | 2011-02-03 |              0 |        6 |
|  7 |   721 | 2008-12-03 |              0 |        2 |
|  8 |   724 | 2011-03-02 |              0 |        1 |
|  9 |   694 | 2011-02-16 |              0 |        2 |
+----+-------+------------+----------------+----------+

Кто-нибудь знает, как с этим справиться?

Ответы [ 2 ]

0 голосов
/ 17 февраля 2019

Рассмотрим следующее:

DROP TABLE IF EXISTS my_table;

CREATE TABLE my_table
(id SERIAL PRIMARY KEY
,patient_id INT NOT NULL
,dt DATE
);

INSERT INTO my_table VALUES
( 1,649,'0000-00-00'),
( 2,649,'2010-09-23'),
( 3,649,'2010-09-23'),
( 4,649,'2010-09-23'),
( 5,649,'2010-09-23'),
( 6,649,'2010-09-23'),
( 7,649,'2010-09-23'),
( 8,649,'2010-09-23'),
( 9,649,'2010-09-23'),
(10,649,'2010-09-23'),
(12,673,'2010-08-16'),
(13,676,'2010-07-05'),
(14,649,'2010-02-17'),
(15,649,'2010-02-17'),
(16,676,'2010-07-05'),
(17,718,'2010-12-01'),
(18,720,'0000-00-00'),
(19,720,'0000-00-00'),
(20,649,'2010-02-17'),
(21,649,'2010-02-17'),
(22,649,'2010-02-17'),
(23,649,'2010-02-17'),
(24,649,'2010-02-17'),
(25,719,'2008-12-03'),
(26,719,'2008-12-03'),
(27,721,'2008-12-03'),
(28,649,'2010-02-16'),
(29,649,'2010-02-16'),
(30,720,'2011-02-03'),
(31,720,'2011-02-03'),
(32,720,'2011-02-03'),
(33,720,'2011-02-03'),
(34,720,'2011-02-03'),
(35,676,'2010-07-05'),
(36,718,'2010-12-01'),
(37,719,'2008-12-03'),
(38,721,'2008-12-03'),
(39,649,'2010-02-16'),
(40,673,'2010-08-16'),
(41,694,'2011-02-16'),
(42,649,'2010-02-16'),
(43,649,'2010-10-23'),
(44,649,'2010-02-16'),
(45,649,'2010-02-16'),
(46,649,'2010-02-16'),
(47,724,'2011-03-02'),
(48,724,'2011-03-02'),
(49,694,'2011-02-16'),
(50,694,'2011-02-16'),
(51,649,'2010-02-16');

Запрос:

SELECT x.*
     , y.rev 
  FROM my_table x 
  JOIN 
     ( SELECT MIN(id) id, COUNT(*)-1 rev FROM my_table GROUP BY patient_id ) y 
    ON y.id = x.id 
 ORDER 
    BY id;
+----+------------+------------+-----+
| id | patient_id | dt         | rev |
+----+------------+------------+-----+
|  1 |        649 | 0000-00-00 |  25 |
| 12 |        673 | 2010-08-16 |   1 |
| 13 |        676 | 2010-07-05 |   2 |
| 17 |        718 | 2010-12-01 |   1 |
| 18 |        720 | 0000-00-00 |   6 |
| 25 |        719 | 2008-12-03 |   2 |
| 27 |        721 | 2008-12-03 |   1 |
| 41 |        694 | 2011-02-16 |   2 |
| 47 |        724 | 2011-03-02 |   1 |
+----+------------+------------+-----+
0 голосов
/ 17 февраля 2019

Не ответ;слишком долго для комментария ...

Вот что я считаю правильным подходом к первой части проблемы ...

DROP TABLE IF EXISTS method;

CREATE TABLE method
(method_id SERIAL PRIMARY KEY
,method VARCHAR(255) NOT NULL UNIQUE
);

INSERT INTO method VALUES 
(  1,  "Keine Operation"),
(  2,  "Bioenterics Intragastric Ballon (BIB)"),
(  3,  "Gastric Banding"),
(  4,  "Roux-en-Y Gastric Bypass"),
(  5,  "Roux-en-Y Gastric Bypass banded"),
(  6,  "Scopinaro"),
(  7,  "Duodenal Switch (DS)"),
(  8,  "Sleeve Resection"),
(  9,  "Gastric Pacemaker"),
( 10,  "Billroth II"),
( 12,  "Gastroplasty"),
( 13,  "Fobi / Capella Bypass"),
( 14,  "Larrad"),
( 15,  "Santoro"),
( 16,  "DJB"),
( 17,  "TOGA"),
( 18,  "Endobarrier"),
( 19,  "Gastric Plication"),
( 20,  "Stomaphyx"),
( 21,  "Omega Loop Bypass"),
( 22,  "Omega Loop Bypass banded"),
( 23,  "Long Limb Bypass"),
( 24,  "Distal Very Long Gastric Bypass (Thurnheer)"),
( 25,  "Endoscopic Sclerosation"),
( 26,  "Swedish Adjustable Gastric Bypass (SAGB)"),
( 27,  "Vertical Banded Gastroplasty (VBG)"),
( 28,  "Plastic Abdominal Wall Reconstruction (PAWR)"),
( 29,  "Inner Hernia Repair"),
( 30,  "Single Anastomosis Duodeno-Ileal Bypass with Sleeve Gastrectomy (SADI-S)"),
(100,  "Anderes OP-Verfahren");

DROP TABLE IF EXISTS therapy;

CREATE TABLE therapy
(therapy_id SERIAL PRIMARY KEY
,method_id INT NOT NULL
,date DATE NOT NULL
);

INSERT INTO therapy (SELECT NULL,1+(RAND()*30),'2015-01-01' + INTERVAL RAND()*100 DAY);
INSERT INTO therapy SELECT NULL,1+(RAND()*30),'2015-01-01' + INTERVAL RAND()*100 DAY FROM therapy; -- Repeat this line a few times...

SELECT m.*
     , COUNT(t.therapy_id) total 
  FROM method m 
  LEFT 
  JOIN therapy t 
    ON t.method_id = m.method_id 
 GROUP 
    BY m.method_id;
+-----------+--------------------------------------------------------------------------+-------+
| method_id | method                                                                   | total |
+-----------+--------------------------------------------------------------------------+-------+
|         1 | Keine Operation                                                          |    38 |
|         2 | Bioenterics Intragastric Ballon (BIB)                                    |    79 |
|         3 | Gastric Banding                                                          |    72 |
|         4 | Roux-en-Y Gastric Bypass                                                 |    73 |
|         5 | Roux-en-Y Gastric Bypass banded                                          |    59 |
|         6 | Scopinaro                                                                |    66 |
|         7 | Duodenal Switch (DS)                                                     |    58 |
|         8 | Sleeve Resection                                                         |    67 |
|         9 | Gastric Pacemaker                                                        |    64 |
|        10 | Billroth II                                                              |    67 |
|        12 | Gastroplasty                                                             |    55 |
|        13 | Fobi / Capella Bypass                                                    |    68 |
|        14 | Larrad                                                                   |    61 |
|        15 | Santoro                                                                  |    69 |
|        16 | DJB                                                                      |    69 |
|        17 | TOGA                                                                     |    60 |
|        18 | Endobarrier                                                              |    67 |
|        19 | Gastric Plication                                                        |    77 |
|        20 | Stomaphyx                                                                |    65 |
|        21 | Omega Loop Bypass                                                        |    86 |
|        22 | Omega Loop Bypass banded                                                 |    83 |
|        23 | Long Limb Bypass                                                         |    69 |
|        24 | Distal Very Long Gastric Bypass (Thurnheer)                              |    58 |
|        25 | Endoscopic Sclerosation                                                  |    67 |
|        26 | Swedish Adjustable Gastric Bypass (SAGB)                                 |    69 |
|        27 | Vertical Banded Gastroplasty (VBG)                                       |    71 |
|        28 | Plastic Abdominal Wall Reconstruction (PAWR)                             |    69 |
|        29 | Inner Hernia Repair                                                      |    72 |
|        30 | Single Anastomosis Duodeno-Ileal Bypass with Sleeve Gastrectomy (SADI-S) |    77 |
|       100 | Anderes OP-Verfahren                                                     |     0 |
+-----------+--------------------------------------------------------------------------+-------+

Остальная часть этой проблемы -проблема с отображением, более корректно обработанная в коде приложения.

Для следующей части проблемы нам нужно увидеть MCVE вместе с желаемым результатом (см. Почему я должен предоставить MCVE длячто мне кажется очень простым SQL-запросом ).Очевидно, не используйте реальные данные пациента для этого!

...