sql - 每个月的新 ID
问题描述
我对此思考了很多,但找不到答案,而且我不太擅长 SQL 查询。
表格1:
id | month
_________________
123 | 11/2015
124 | 11/2015
123 | 12/2015
124 | 12/2015
125 | 12/2015
我需要一个查询,它可以为我提供每月新 ID 的数量。在示例中,我想输出:
#id | month
_________________
2 | 11/2015
1 | 12/2015
因为“123”和“124”在 11/2015,所以 2 因为“125”不在 12/2015,所以 1
抱歉,如果我不清楚,我尝试使用自我连接和滞后进行查找,但找不到答案
谢谢
解决方案
可以not exists
用来过滤掉id
上个月已经出现的s,然后聚合:
select count(*) id_cnt, t.month
from mytable t
where not exists (
select 1
from mytable t1
where t1.id = t.id
and t1.month < t.month
)
group by t.month
推荐阅读
- javascript - 我试图根据距离查找特定路线的价格,但是每当我放置地点时,if循环返回else而不是价格
- r - 我收到错误“knn 错误(train = prc_train, test = prc_test, cl = prc_train_labels, : no missing values are allowed”
- sql - 设计一个表来存储单个实体的不同值?
- c++ - 如何使用 C++ 复制一些具有不同扩展名的文件
- excel - 删除不在两个其他单元格之间的行
- c++ - 使用 to_string、reverse、stoi 组合的 C++ 反转数给出运行时错误实例超出范围
- android - 片段重新创建后 ViewModel 不保留数据
- python - 如何在 Windows 10 上连接 web2py 和 mysql
- python - Access 已打开 Excel 和 Internet Explorer 以获取数据
- flutter - mailto:Flutter for Web 的链接