我有一張這樣的機器安裝表:
installationID, machineID, installed_at, uninstalled_at
A, 1, 2020-01-01, Null
B, 2, 2020-01-01, 2020-01-02
C, 3, 2020-01-02, Null
D, 2, 2020-01-04, Null
我需要一個查詢,返回每天安裝的機器數量。這樣地:
Date, installed
2020-01-01, 2
2020-01-02, 3
2020-01-03, 2
2020-01-04, 3
我知道給定一個日期,比如說'2020-01-03',我可以得到安裝機器的數量,如下所示:
SELECT date, count(machineID)
from installs
where installed_at >= '2020-01-03'
and (uninstalled_at is Null or uninstalled_at <= '2020-01-03')
但是,我不知道如何查詢,這樣我就可以在一次查詢中得到所有日期的結果。
使用從2020-01-01到2020-01-15生成的序列。根據需要調整“<=/>”。