我想向表中添加其他(缺失)行。 現有情況:我有一張病人表,其中有一個註冊日期,我計算了病人已經註冊了多長時間(「TimeInProgram」)。現在,我彙總了這一點,以獲取當前處於此期間的患者人數(請參閱計數 - >屬性「患者」)。最後,我總結了一個窗口函數來獲取在特定時期內已經有WERE的患者人數。SQL:添加缺少的行
SELECT aggregation.*, SUM("Patients") OVER() "All", SUM("Patients") OVER (ORDER BY "TimeInProgram" DESC ROWS UNBOUNDED PRECEDING) "PatientsCounterInTime" FROM (
SELECT COUNT(DISTINCT "PatientID") "Patients", "TimeInProgram" FROM (
SELECT
pat."PatientID",
ROUND(((DAYS_BETWEEN(pat."RegisteredOnDate", NOW())+1)/7.0), 0, 'ROUND_UP') "TimeInProgram"
FROM t_patient pat
ORDER BY "PatientID"
) data
GROUP BY "TimeInProgram"
ORDER BY "TimeInProgram"
) aggregation
結果,你可以在這裏看到:
PatientsCounterInTime PLUS以下行的患者在以下row.You的PatientsCounterInTime的值可以看到的問題:有一些「 TimeInProgram「 - 周如第4周或第5周缺失,因爲此時沒有患者在此期間。所以我想添加缺少的行。 「PatientsCounterInTime」的值在第4周,第5周應爲162.「患者」當然爲0.
我使用HANA作爲數據庫。
提前致以親切的問候和感謝。
UPDATE:
SELECT "TiP" "TimeInProgram", "Patients", "PatientsCounterInTime" FROM (
SELECT aggregation.*, SUM("Patients") OVER() "All", SUM("Patients") OVER (ORDER BY "TimeInProgram" DESC ROWS UNBOUNDED PRECEDING) "PatientsCounterInTime" FROM (
SELECT COUNT(DISTINCT "PatientID") "Patients", "TimeInProgram" FROM (
SELECT
pat."PatientID",
ROUND(((DAYS_BETWEEN(pat."RegisteredOnDate", NOW())+1)/7.0), 0, 'ROUND_UP') "TimeInProgram"
FROM "t_patient" pat
ORDER BY "PatientID"
) data
GROUP BY "TimeInProgram"
ORDER BY "TimeInProgram"
) aggregation
)
RIGHT JOIN "t_time_in_program" t ON "TimeInProgram" = t."TiP";
(以下行PatientsCounterInTime PLUS患者是下一行的PatientsCounterInTime的值。)
您需要一個包含所有星期的日曆表,基本上是一個單列,其中包含1到48,並且在該列上加上LEFT JOIN – Mihai
除了Mihai所說的內容之外,您還必須使用LEAD或LAG來獲取最後一個部分計數器時間加入時(SINCE在離開後會有一個空值) – sagi
謝謝你的幫助!我加入了桌面並更新了上面的帖子。我不確定現在如何使用LEAD/LAG填寫缺席專欄的數據。任何提示再次? @sagi – ScientiaEtVeritas