2011-03-18 79 views
1

我绞尽脑汁广泛搜索以找到解决方案,并且我怀疑我可能没有清楚地询问问题,请耐心等待。SQL查询根据组中缺少值来过滤记录

我必须建立几个查询,在以下基础上筛选记录。尽管涉及提取数据的多个表格我会坚持基本要求。

以下是样本值:

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)关键决定可能包含“附加信息” - 在上面的例子中只有键= 123将匹配

这似乎几乎可能,就像我有答案在某处浮动,我不能完全理解它。还是这种一厢情愿的想法?

我曾尝试以下

select key from table where decision not in (select key from table where decision <> "Complete") 

这让我想决策的结果=完整。但是,最终的选择至少包含至少三个连接,我怀疑性能会变差。查询将在Oracle 11g上执行。

如果有人有任何建议可以帮助我摆脱这种想法,我会深表感谢。

+0

我不明白。 '这些查询将在Oracle 11g'上执行。那你为什么用'mysql'标签来标记问题呢?这是一个错误还是标签实际相关? – 2011-03-18 13:52:20

回答

1

对于第一个问题

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` 
0

1)键,其中只有判定为 “完成” - 在上面的例子仅重点= 254将匹配

select key 
    from table 
group 
    by key 
having min(decision) = 'Complete' 
    and max(decision) = 'Complete' 

或@nick rulez用以下修改ication(使它在Oracle运行以及):

having count(decision) = sum(case when decision = 'Complete' then 1 else 0 end) 

2)键,其中决定可能含有 “附加信息” - 在上面的例子中唯一的关键= 123将匹配

select distinct key 
    from table 
where decision = 'Additional info';