我正在尝试过滤 SQL 数据库。
每行代表一个用户,我关注的主列标题为 last_visited 并格式化为… 2009-06-17 12:15:32。
过去一天/一周/一个月有多少用户访问过?
像:
SELECT COUNT(*) AS USERS_TODAY
FROM parts_users
Where updated_at > (NOW()-7)
那么是列文本还是日期时间?如果是前者:
SELECT COUNT(*) AS USERS_TODAY
FROM parts_users
WHERE CONVERT(datetime, updated_at, 120) > DATEADD(day, -7, GETDATE())
如果是后者,则省略 CONVERT。
或者,您可能会发现类似的东西很有用:
SELECT CONVERT(datetime, LEFT(updated_at, 10), 120) AS DATE_ONLY,
COUNT(*) AS USERS_TODAY
FROM parts_users
WHERE CONVERT(datetime, updated_at, 120) > DATEADD(day, -7, GETDATE())
GROUP BY CONVERT(datetime, LEFT(updated_at, 10), 120)
如何在 iReport 中使用 SQL 查询同一项目的多个 SUM
SELECT
SUM(case when (NOW()-1) <= datetime then 1 else 0 end) as 'today',
SUM(case when (NOW()-7) <= datetime then 1 else 0 end) as 'thisweek',
COUNT(*) as 'thismonth'
FROM
table
WHERE
(NOW()-30) <= datetime AND datetime <= NOW()