按天计数
获取每日计数(Oracle 语法):
-- Oracle
SELECT
TRUNC(CallDate) AS Dt,
COUNT(*) AS DailyCount
FROM MOPActivity
WHERE CallDate > DATE '2013-01-01' AND CallDate < '2013-02-01'
GROUP BY TRUNC(CallDate)
如果您使用的是 MySQL,请将TRUNC(CallDate)
上面的两个实例替换为DATE(CallDate)
:
-- MySQL
SELECT
DATE(CallDate) AS Dt,
COUNT(*) AS DailyCount
FROM MOPActivity
WHERE CallDate > DATE '2013-01-01' AND CallDate < '2013-02-01'
GROUP BY DATE(CallDate)
上面的查询将仅报告有呼叫活动的日期,因此,例如,如果在 2013 年 1 月 6 日星期日没有任何呼叫,则该日期不会有一行。
月平均
Oracle 将允许您将聚合函数嵌套到最多两个级别,这可以通过执行以下操作来获得每月AVG
平均值COUNT
:
-- Oracle
SELECT AVG(COUNT(*)) AS MonthlyAverage,
FROM MOPActivity
WHERE CallDate > DATE '2013-01-01' AND CallDate < '2013-02-01'
GROUP BY TRUNC(CallDate)
MySQL 不支持这一点,因此如果您使用 MySQL,您可以将每日计数查询推送到子查询中,并让外部查询计算平均值:
-- MySQL
SELECT AVG(DailyCount) AS MonthlyAverage
FROM (
SELECT
DATE(CallDate) AS Dt,
COUNT(*) AS DailyCount
FROM MOPActivity
WHERE CallDate > DATE '2013-01-01' AND CallDate < '2013-02-01'
GROUP BY DATE(CallDate)
) DayCounts