0

maxdateを取得するためのクエリがあります。

SELECT a.client_id AS client_id
   , a.patientcount AS patientcount
   , received_date AS maxRecDate
FROM
(
   SELECT DISTINCT CLIENT_ID
   , PATIENT_ID
   , count(*) over (partition by client_id, patient_id) AS patientcount
   from f_accession_daily
) AS a
JOIN F_ACCESSION_DAILY AS f ON a.CLIENT_ID = f.CLIENT_ID
   AND a.PATIENT_ID = f.PATIENT_ID
GROUP BY f.RECEIVED_DATE, a.CLIENT_ID, a.patientcount
HAVING f.RECEIVED_DATE = MAX(f.received_date)

同じクエリで、min(f.received_date)も返したいと思います。

これを行う方法はありますか?おそらくサブクエリで?

4

2 に答える 2

2

何かが足りないかもしれませんが、なぜあなたがグループ化しているのかわかりませんf.RECEIVED_DATE

クエリは次のようになると思います。

SELECT a.client_id AS client_id
   , a.patientcount AS patientcount
   , max(f.received_date) AS maxRecDate
   , min(f.received_date) AS minRecDate
FROM
(
   SELECT DISTINCT CLIENT_ID
   , PATIENT_ID
   , count(*) over (partition by client_id, patient_id) AS patientcount
   from f_accession_daily
) AS a
JOIN F_ACCESSION_DAILY AS f ON a.CLIENT_ID = f.CLIENT_ID
   AND a.PATIENT_ID = f.PATIENT_ID
GROUP BY a.CLIENT_ID, a.patientcount

私はこれをテストしていませんが、うまくいくはずだと思います。

于 2012-10-19T06:00:11.593 に答える
1

クエリにOR条件を追加して、MIN日付もチェックします。問題を解決する

SELECT a.client_id AS client_id
, a.patientcount AS patientcount
, received_date AS maxRecDate
FROM
(
SELECT DISTINCT CLIENT_ID
, PATIENT_ID
, count(*) over (partition by client_id, patient_id) AS patientcount
from f_accession_daily
) AS a
JOIN F_ACCESSION_DAILY AS f ON a.CLIENT_ID = f.CLIENT_ID
AND a.PATIENT_ID = f.PATIENT_ID
GROUP BY f.RECEIVED_DATE, a.CLIENT_ID, a.patientcount
HAVING f.RECEIVED_DATE = MAX(f.received_date) 
OR f.RECEIVED_DATE = MIN(f.received_date) -- Newly added condition
于 2012-10-19T08:53:40.527 に答える