显示过去 X 天的数据

你好
我正在尝试运行一个报告,显示过去 30 天的访问量,所以我使用了这个查询,但显然是错误的:

select  distinct user_id, max(visited_at) as visited_at from user_visits
WHERE CAST(visited_at  AS DATE) >= GETDATE() -30
group by user_id

我也测试了这个条件
WHERE CAST(visite d_at AS DATE) >= CAST(GETDATE() -30 AS DATE)

这种条件有可能吗?
谢谢

1 个赞

试试

SELECT
  user_id,
  COUNT(*) AS visits
FROM user_visits
WHERE visited_at > CURRENT_DATE - 30
GROUP BY 1
ORDER BY 2 DESC
2 个赞

谢谢 @Falco,它运行正常

2 个赞

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.