1

I got confused about the algorithm and way to get the latest index of Group by. I have a query like this.

SELECT hasil_kmbg AS RESULT,
   EXTRACT(MONTH
           FROM tgl_check) AS MONTH,
   EXTRACT(YEAR
           FROM tgl_check) AS YEAR
FROM perkembangan
WHERE no_pasien=11
GROUP BY MONTH,
         id_kmbg DESC

and here is result of the query

|Result|Month|Year|
-------------------
|3     |1    |2013|
|1     |1    |2013|
|5     |1    |2013|
|1     |2    |2013|
|1     |3    |2013|
and so on

the question is, how I just get Result which is Result is 3 in Month 1? I didn't want to show other result int Month 1 except 3 (the latest, I order it by desc).

4

2 回答 2

0

id_kmbg由于您已包含在该group by子句中,您每月将获得多行。我想你想要这个max()功能:

select max(hasil_kmbg) as Result, EXTRACT(MONTH FROM tgl_check) as Month,
       EXTRACT(YEAR FROM tgl_check) as Year
from perkembangan
where no_pasien=11
group by Month, Year;
于 2013-07-24T00:58:23.860 回答
0

我认为你正在寻找这个:

SELECT hasil_kmbg AS RESULT,
       EXTRACT(MONTH
               FROM tgl_check) AS MONTH,
       EXTRACT(YEAR
               FROM tgl_check) AS YEAR
FROM perkembangan
WHERE no_pasien=11
ORDER BY id_kmbg DESC
LIMIT 1

我的预感是你不需要这里的 GROUP BY(因为你是通过 ID DESC 订购的)。

如果您需要分组依据,您可能需要修改选择 hasil_kmbg 的方式。

于 2013-07-24T01:03:58.790 回答