Mysql 基于组中缺少值筛选记录的SQL查询
我绞尽脑汁,在谷歌上搜索了很多问题,想找到一个解决方案,我怀疑我可能没有明确地提出这个问题,所以请耐心听我说 我必须构建两个查询,在以下基础上过滤记录。虽然提取数据涉及多个表,但我将坚持基本要求 以下是示例值:Mysql 基于组中缺少值筛选记录的SQL查询,mysql,sql,Mysql,Sql,我绞尽脑汁,在谷歌上搜索了很多问题,想找到一个解决方案,我怀疑我可能没有明确地提出这个问题,所以请耐心听我说 我必须构建两个查询,在以下基础上过滤记录。虽然提取数据涉及多个表,但我将坚持基本要求 以下是示例值: Key | Decision 123 | Complete 123 | Additional info 123 | Something 123 | Complete . . . 254 | Complete 254 | Complete 254 |
Key | Decision
123 | Complete
123 | Additional info
123 | Something
123 | Complete
.
.
.
254 | Complete
254 | Complete
254 | Complete
.
.
.
基于上述数据,我可以按键进行选择和分组,并做出如下决定,以获得数据集:
Key | Decision
123 | Complete
123 | Additional info
123 | Something
.
.
.
254 | Complete
.
.
.
我需要的实际数据有两种类型,它们是必须构建的分离查询
1键,其中唯一的决定是完整的-在上述示例中,只有键=254匹配
2个键,其中决策可能包含其他信息-在上述示例中,只有Key=123匹配
这似乎几乎是可能的,好像我的答案飘浮在某个地方,我不能完全理解它。还是一厢情愿
我确实尝试了以下方法
select key from table where decision not in (select key from table where decision <> "Complete")
这就得到了我想要的Decision=Complete的结果。但是,由于最终选择至少包含三个联接,我怀疑性能会很差。查询将在Oracle 11g上执行
如果有人能给我一些建议,帮助我摆脱这种思维定势,我将不胜感激。关于第一个问题
select `key` from your_table
group by key
having count(decision) = sum(decision="complete")
第二个
select `key` from your_table
where decision = 'Additional Info'
group by `key`
关于第一个问题
select `key` from your_table
group by key
having count(decision) = sum(decision="complete")
第二个
select `key` from your_table
where decision = 'Additional Info'
group by `key`
1键,其中唯一的决定是完整的-在上述示例中,只有键=254匹配
或者@nick rulez通过以下修改编写的内容,使其也在Oracle上运行:
having count(decision) = sum(case when decision = 'Complete' then 1 else 0 end)
2个键,其中决策可能包含其他信息-在上述示例中,只有Key=123匹配
1键,其中唯一的决定是完整的-在上述示例中,只有键=254匹配
或者@nick rulez通过以下修改编写的内容,使其也在Oracle上运行:
having count(decision) = sum(case when decision = 'Complete' then 1 else 0 end)
2个键,其中决策可能包含其他信息-在上述示例中,只有Key=123匹配
我不明白。查询将在Oracle 11g上执行。那你为什么用mysql标签来标记这个问题呢?这是个错误还是标签真的相关?我不明白。查询将在Oracle 11g上执行。那你为什么用mysql标签来标记这个问题呢?这是一个错误还是标签实际相关?