给出样本表
Field 1 | Field 2 | Field 3
------------------------------
class1 | 02-09-2016 | 10.00
Class1 | 03-09-2016 | 09.20
Class1 | 04-09-2016 | 08.00
class2 | 02-09-2016 | 10.00
Class2 | 03-09-2016 | 10.00
Class2 | 04-09-2016 | 10.00
class3 | 02-09-2016 | 33.32
Class3 | 03-09-2016 | 40.12
Class3 | 04-09-2016 | 10.20
我想查询Field1在一段时间内没有变化的Field1。在这种情况下,我只需要选择class2,其中Field3在02-09-2016之间没有变化。 2016年4月9日。
有人可以帮忙吗?
答案 0 :(得分:0)
如果我理解正确,您只需查看min()
和max()
值并进行比较:
select field1
from t
group by field1
having min(field3) = max(field3);
答案 1 :(得分:0)
表格设置:
create table so1 (field1 varchar2(100), field2 varchar2(100), field3 varchar2(100))
insert into so1
select *
from (
select 'class1', '02-09-2016', '10.00' from dual union all
select 'Class1', '03-09-2016', '09.20' from dual union all
select 'Class1', '04-09-2016', '08.00' from dual union all
select 'class2', '02-09-2016', '10.00' from dual union all
select 'Class2', '03-09-2016', '10.00' from dual union all
select 'Class2', '04-09-2016', '10.00' from dual union all
select 'class3', '02-09-2016', '33.32' from dual union all
select 'Class3', '03-09-2016', '40.12' from dual union all
select 'Class3', '04-09-2016', '10.20' from dual
)
查询:
select field1, field2, field3
from (
select s.*,
count(distinct field3) over (partition by upper(field1)) cnt
from so1 s
)
where cnt = 1
查询选择符合条件的所有行/列。
答案 2 :(得分:0)
那么你想要的只是找到field1,其中只有一个不同的field3值?
使用分组和Having子句
完成 Select Distinct field1 from table t
Where not exists
(Select * from table
where field1 = t.field1
having count(distinct field3) > 1)