您可以使用以下CASE
语句COUNT
:
SELECT task.date_created,
count(1) opened,
count(case when is_closed = 'true' then 1 end) closed
FROM task
GROUP BY task.date_created
ORDER BY task.date_created
SQL 小提琴演示
鉴于您的意见,这里是使用一对夫妇的方法Common Table Expressions
:
WITH OPENED AS (
SELECT date_created, count(1) opened
FROM task
GROUP BY date_created
) ,
CLOSED AS (
SELECT date_updated, count(1) closed
FROM task
WHERE is_closed = 'true'
GROUP BY date_updated
)
SELECT D.YourDateField, o.opened, c.closed
FROM YourDateTable D
LEFT JOIN Opened O ON D.YourDateField = O.Date_Created
LEFT JOIN CLosed C ON D.YourDateField = C.Date_Created
正如 Gordon 指出的那样,aFULL OUTER JOIN
也可以。我只是更喜欢使用日期表来播种。创建表一次,然后在需要的任何地方使用它。