mysql - MySQL查询以识别每月只有一个特定值的记录
问题描述
正如标题中所指定的,我需要编写一个查询来获取结果特定的行(序列号列表),这些行在每个月中只有一个状态是“过期”状态(月份将在 end_time 上计算)。例如:我的表是
sr_num, start_time, end_time , renew_date, status
s1 , 15-01-2016, 15-01-2017, 15-01-2016, purchase
s1 , 15-01-2016, 15-01-2017, 15-01-2016, expired
s2 , 15-01-2016, 15-01-2017, 15-01-2016, purchase
s2 , 15-01-2016, 15-01-2017, 15-01-2016, expired
s2 , 20-01-2017, 20-01-2018, 20-01-2017, renew
s3 , 15-01-2016, 15-01-2017, 15-01-2016, purchase
s3 , 15-01-2016, 15-01-2018, 10-01-2017, extends
CREATE TABLE test (
sr_number CHAR(20),
start_time DATE NOT NULL,
end_time DATE,
renew_date DATE,
status CHAR(20)
);
INSERT INTO `test`
VALUES
('s1', '2016-01-15', '2017-01-15', '2016-01-15', 'purchase'),
('s1', '2016-01-15', '2017-01-15', '2016-01-15', 'expired'),
('s2', '2016-01-15', '2017-01-15', '2016-01-15', 'purchase'),
('s2', '2016-01-15', '2017-01-15', '2016-01-15', 'expired'),
('s2', '2017-01-20', '2018-01-20', '2017-01-20', 'renew'),
('s3', '2016-01-15', '2017-01-15', '2016-01-15', 'purchase'),
('s3', '2016-01-15', '2018-01-15', '2017-01-10', 'extends');
条件:
- “按 sr_num、end_times 分组”
- 状态必须仅在每个月过期
结果应该只有一行s1, 15-01-2016, 15-01-2017, 15-01-2016, expired
解决方案
推荐阅读
- javascript - 将内存地址位置作为html的href中的链接
- vba - 在 Excel 的两个工作簿中查找匹配数据,并在大型数据集上格式化匹配结果
- python - 哪一个是高效的,使用 sql 连接查询,或使用 pandas 合并查询?
- azure - 暂停 Azure 存储同步服务
- php - 在 laravel 中对字符串调用成员函数 format()
- assembly - 使用 msdos 文件编码时的汇编语言问题
- r - R - 替换文本中的单词/短语
- spring-boot - Kubernetes Secret TLS 证书 P12 和 Spring Boot 部署不起作用
- php - 脚本中的 PHP MySQL 语法错误,但直接输入 MySQL 时语句有效
- java - 即使遇到问题,也要在跨区域内提交