が、このような何か:
SELECT L.PATID_CD,
MAX(L.RESULT_DE)
KEEP (DENSE_RANK LAST ORDER BY CASE WHEN L.COLLECTION_DT < PL.NOTED_DATE
THEN L.COLLECTION_DT END NULLS FIRST)
AS HEMOG_BEFORE,
MAX(CASE WHEN L.COLLECTION_DT < PL.NOTED_DATE THEN L.COLLECTION_DT END)
AS DATE_TAKEN_BEFORE,
MAX(L.RESULT_DE)
KEEP (DENSE_RANK FIRST ORDER BY CASE WHEN L.COLLECTION_DT >= PL.NOTED_DATE
THEN L.COLLECTION_DT END NULLS LAST)
AS HEMOG_AFTER,
MIN(CASE WHEN L.COLLECTION_DT >= PL.NOTED_DATE THEN L.COLLECTION_DT END)
AS DATE_TAKEN_AFTER
FROM LAB L
INNER JOIN PROBLEM_LIST_CN PL
ON (L.PATID_CD = PL.PAT_ID)
WHERE L.PROCEDURE_DE = 'HEMOGLOBIN'
GROUP BY L.PATID_CD;
か:
SELECT *
FROM (
SELECT t.*,
ROW_NUMBER() OVER (PARTITION BY platin_cd ORDER BY date_after) AS rid
FROM (
SELECT l.platin_cd,
LAG(l.result_de ) OVER (PARTITION BY l.platin_cd
ORDER BY collection_dt) AS hemog_before,
LAG(l.collection_dt) OVER (PARTITION BY l.platin_cd
ORDER BY collection_dt) AS date_before,
l.result_de AS hemog_after,
l.collection_dt AS date_after,
pl.noted_date
FROM LAB L
INNER JOIN PROBLEM_LIST_CN PL
ON (L.PATID_CD = PL.PAT_ID)
WHERE L.PROCEDURE_DE = 'HEMOGLOBIN'
)
WHERE date_after >= noted_date
)
WHERE rid = 1;
出典
2017-01-26 09:28:24
MT0
http://meta.stackoverflow.com/questions/285551あなたの望む結果の代わりに何を得ているのですか?/ 285557#285557 –
あなたの望む結果の代わりに何を得ていますか? – salvolds
'LAG()'と 'LEAD()'解析関数を調べるべきです。 – Boneist