查找数据库中重复的值的数据,having的使用,count(1),sum等聚会函数

时间:2023-03-09 19:28:36
查找数据库中重复的值的数据,having的使用,count(1),sum等聚会函数
通过having代替where来使用count(1),sum()等函数
譬如如下数据
id value
1 2
1 3
2 3
3 5
3 6 可以写个语句统计value的分组
select id,sum(value) from table group by id having sum(value)>=5

在这里,可以省略前面的sum(value)。成这样

select id from table group by id having sum(value)>=5
这样的结果就是
1 5
3 11 其实这句的意思就是
select id,sum(value) from table where sum(value)>=5 group by id 
但是oracle中,计算字段没法当作条件来用,所以就用having 来表示
count(*)也是一个道理的,当然什么avg,max,min之类的聚合函数也同样
比如:存在这个表a1。

A1 B1
2   4
2   4
12 41
12 231
112 31。

要想求出重复的字段。可以用这种方法:

select a1.b1,count(a1.b1)
from a1
group by a1.b1
having count(a1.b1)>=2

最后结果是:

B1 COUNT(A1.B1)
 4        2