获取按日期分组的实例的 Count(*)。
Get Count(*) of instance where it is grouped by date.
SQL 查询,其中 date_created
将返回计数
SELECT COUNT(*) FROM records_call
WHERE source_address = '1234'
AND date_created >= '2015-02-24 12:00:00'
我如何获得按日期排序的计数?
这应该适用于大多数数据库:
SELECT date_created, COUNT(*) FROM records_call
WHERE source_address = '1234'
AND date_created >= '2015-02-24 12:00:00'
GROUP BY date_created
ORDER BY date_created
如果你想在两列中得到每个日期的计数(*)结果,你需要在 select 中包含日期并在 group by 子句中使用它:
SELECT DATE(date_created) AS "Date Created", COUNT(*) AS "Count"
FROM records_call
WHERE source_address = '1234'
AND date_created >= '2015-02-24 12:00:00'
GROUP BY DATE(date_created)
ORDER BY DATE(date_created)
由于 date_created
包含一个时间组件,您可能希望将其转换为一个日期,除非所有日期都具有完全相同的时间 - 如何执行此操作取决于您使用的数据库,它可能是cast(date_created as date)
、DATE(date_created)
或类似的东西。
SQL 查询,其中 date_created
将返回计数SELECT COUNT(*) FROM records_call
WHERE source_address = '1234'
AND date_created >= '2015-02-24 12:00:00'
我如何获得按日期排序的计数?
这应该适用于大多数数据库:
SELECT date_created, COUNT(*) FROM records_call
WHERE source_address = '1234'
AND date_created >= '2015-02-24 12:00:00'
GROUP BY date_created
ORDER BY date_created
如果你想在两列中得到每个日期的计数(*)结果,你需要在 select 中包含日期并在 group by 子句中使用它:
SELECT DATE(date_created) AS "Date Created", COUNT(*) AS "Count"
FROM records_call
WHERE source_address = '1234'
AND date_created >= '2015-02-24 12:00:00'
GROUP BY DATE(date_created)
ORDER BY DATE(date_created)
由于 date_created
包含一个时间组件,您可能希望将其转换为一个日期,除非所有日期都具有完全相同的时间 - 如何执行此操作取决于您使用的数据库,它可能是cast(date_created as date)
、DATE(date_created)
或类似的东西。