1

我在 MySQL 中有下表,

+-------------+-----------+-----------+----------+-----------+------------------+-------------+--------+
| ServiceCode | ser_count | FirstName | LastName | PatientID | HealthCardNumber | DateOfBirth | Gender |
+-------------+-----------+-----------+----------+-----------+------------------+-------------+--------+
| AAAA        |         1 | krishna   | Mangeter |         1 | ABC123           | 1982-02-02  | M      |
| BBBB        |         4 | Pinthna   | Paratter |         2 | KBC123           | 1981-02-02  | M      |
| IIII        |         2 | Pinthna   | Paratter |         2 | KBC123           | 1981-02-02  | M      |
| AAAA        |         1 | Pinthna   | Paratter |         2 | KBC123           | 1981-02-02  | M      |
| CCCC        |         1 | Pinthna   | Paratter |         2 | KBC123           | 1981-02-02  | M      |
| BBBB        |         3 | Pelahna   | Kumatter |         3 | SDF123           | 1984-02-02  | M      |
| AAAA        |         2 | Pelahna   | Kumatter |         3 | SDF123           | 1984-02-02  | M      |
| AAAA        |         1 | Pelahna   | Kumatter |         3 | SDF123           | 1984-02-02  | M      |
| BBBB        |         2 | Pelahna   | Kumatter |         3 | SDF123           | 1984-02-02  | M      |
| BBBB        |         2 | Pelahna   | Kumatter |         3 | SDF123           | 1984-02-02  | M      |
| GGGG        |         1 | Manshna   | Bituater |        14 | MKO123           | 1982-02-02  | M      |
| FFFF        |         1 | Manshna   | Bituater |        14 | MKO123           | 1982-02-02  | M      |
| IIII        |         3 | Manshna   | Bituater |        14 | MKO123           | 1982-02-02  | M      |
| GGGG        |         1 | Bahshna   | Vitueter |        15 | ZXS123           | 1982-02-02  | F      |
| IIII        |         1 | Krishhn   | Ishqeter |        16 | VGT123           | 1987-02-02  | M      |
+-------------+-----------+-----------+----------+-----------+------------------+-------------+--------+

我想要上表中的前三名ser_count&ServiceCode并且PatientID想要水平显示它们,如下所示。我尝试在 MySQL 提示符下使用存储过程,但它失败了。另外我不知道存储过程在 iReport/jasperserver 中是否有效。

+-------------+-----------+-----------+----------+-----------+------------------+-------------+--------+------------------------+
| ServiceCode | ser_count | FirstName | LastName | PatientID | HealthCardNumber | DateOfBirth | Gender |Description             |
+-------------+-----------+-----------+----------+-----------+------------------+-------------+--------+------------------------+
| AAAA        |         1 | krishna   | Mangeter |         1 | ABC123           | 1982-02-02  | M      |(1)AAAA                 |
| BBBB        |         4 | Pinthna   | Paratter |         2 | KBC123           | 1981-02-02  | M      |(4)BBBB (2)IIII (1)AAAA |
| BBBB        |         3 | Pelahna   | Kumatter |         3 | SDF123           | 1984-02-02  | M      |(3)BBBB (2)AAAA (2)BBBB |
| GGGG        |         1 | Manshna   | Bituater |        14 | MKO123           | 1982-02-02  | M      |(3)IIII (1)FFFF (1)GGGG |
| GGGG        |         1 | Bahshna   | Vitueter |        15 | ZXS123           | 1982-02-02  | F      |(2)GGGG                 |
| IIII        |         1 | Krishhn   | Ishqeter |        16 | VGT123           | 1987-02-02  | M      |(1)IIII                 |
+-------------+-----------+-----------+----------+-----------+------------------+-------------+--------+------------------------+
4

2 回答 2

2

试试这个

 select * FROM your_table GROUP BY PatientID order by PatientID 

DEMO SQLFIDDLE

编辑。

如果他想要前 3 名,那么只需添加limit子句

     select * FROM Table1 GROUP BY PatientID order by PatientID  limit 3

这里演示

编辑 3

如果您想为每个 PatientID 获得 3 个结果,那么就可以了

     SELECT ServiceCode, ser_count, FirstName,LastName,PatientID,HealthCardNumber,DateOfBirth,Gender
     FROM  (select ServiceCode, ser_count, FirstName,LastName,PatientID,HealthCardNumber,DateOfBirth,Gender,
           @num := if(@group = PatientID, @num + 1, 1) as row_number,
           @group := PatientID as dude
    FROM   Table1
    ORDER BY PatientID, ServiceCode) as ord
    WHERE  row_number <= 3

在此处查看演示

于 2012-12-27T13:57:05.533 回答
1

试试这个:

SELECT ServiceCode, ser_count, FirstName, LastName, PatientID, 
       HealthCardNumber, DateOfBirth, Gender, 
       GROUP_CONCAT(IF(auto <= 3, code, '') ORDER BY ser_count DESC, servicecode SEPARATOR ' ') Description 
FROM (SELECT *, CONCAT('(', ser_count, ')', servicecode) code, 
             IF(@value=(@value:=PatientID), @auto:=@auto+1, @auto:=1) auto
      FROM table1, (SELECT @auto:=1, @value:=0 ) A
      ORDER BY PatientID, ser_count DESC, servicecode) as A 
GROUP BY PatientID;
于 2012-12-27T14:58:21.443 回答