計算預期歸還日期
現在你已經練習過如何加減時間戳記,並使用 interval 進行相對計算,接下來就用這些新技巧來計算某筆租借實際的預期歸還日期。正如你在前面的練習所看到的,rental_duration 表示在被視為逾期前,租借所允許的天數。若要計算 expected_return_date,你需要使用 rental_duration,並把它加到 rental_date 上。
本練習屬於課程
在 PostgreSQL 操作資料的函式
練習說明
- 將
rental_duration轉換為與 1 天INTERVAL相乘的結果。 - 把它加到租借日期上。
動手互動練習
試著完成這個範例程式碼,體驗一下這個練習。
SELECT
f.title,
r.rental_date,
f.rental_duration,
-- Add the rental duration to the rental date
___ '1' day ___ f.___ + ___ AS expected_return_date,
r.return_date
FROM film AS f
INNER JOIN inventory AS i ON f.film_id = i.film_id
INNER JOIN rental AS r ON i.inventory_id = r.inventory_id
ORDER BY f.title;