mysql - Generating monthly report mysql
问题描述
I have a table in which there are 5 columns,
id (auto incrementing number), titleId, version, created_at
A combination of titleId and version is always unique. I want to find out for each day for the past 1 month, how many unique titleIds were present along with the count of how many entries per day. This is because on a given day there might be multiple versions of the same titleId.
select count(*) from titles where created_at >= '2019-08-12 00:00:00' and created_at <= '2019-08-13 00:00:00' will give me total number of titles which came on 12th August
and
select count(distinct titleId) from titles where created_at >= '2019-08-12 00:00:00' and created_at <= '2019-08-13 00:00:00'
will give me the count of unique titles on the 12th August. Is there a way for me to generate the data for the past 30/60 days?
I know I can run this command manually 30 times by changing the date to get the numbers, but was wondering if there is a better way to do this in mysql
解决方案
As long as there is an entry every day, this query should give you the data for each day for the last 30:
select date(created_at) as cdate, count(distinct titleId) as cnt
from titles
where created_at >= cur_date() - interval 30 day
group by cdate
推荐阅读
- javascript - 未捕获的类型错误:无法读取 null Chrome 扩展的属性“点击”
- php - 您可以在查询后使用类似 WHERE 子句的内容吗?
- mysql - MySQL基于两列的多个日期时间间隔
- javascript - 强制填充字符串字段的正则表达式
- eclipse - 无法从 Eclipse 市场空间下载 Kotlin 插件
- html - 使用 CSS 的计划图标
- javascript - 如何在运行函数之前等待变量初始化?(反应)
- angular - 循环依赖 Angular(只是文件名)
- python - pandas DataFrame:删除某些值时如何避免'ValueError:little-endian编译器不支持Big-endian缓冲区'
- javascript - 为什么 Kyle Simpson 的 OLOO 方法感觉像是与 Typescript 潮流背道而驰?