ID 缴费号码 金额
1 139139139 50
2 139139139 40
3 139139131 50
4 139139137 70
5 139139138 100
5 122122122 40
如何找出
1 139139139 50
2 139139139 40
这样的缴费号码相同 金额不同的记录
1 139139139 50
2 139139139 40
3 139139131 50
4 139139137 70
5 139139138 100
5 122122122 40
如何找出
1 139139139 50
2 139139139 40
这样的缴费号码相同 金额不同的记录
是这样?
一般来说id应该是主键了,但是你给的数据有两个5,是笔误还是数据就是这样的
如果有这样的数据你要什么呢
1 139139139 50
2 139139139 40
3 139139139 40a:
1 139139139 50
2 139139139 40b:
1 139139139 50
2 139139139 40c:
1 139139139 50
2 139139139 40
3 139139139 40
id还和那个问题一样,把id换成时间就好。主键很重要。
想到了一个很有趣的方法。oracle 关于集合的集合比一行行处理要快.对了这个方不用主键。加上主键列只是为了让结果看起来更清楚。with aa as
(
select t.id,t.no,t.fee,
count(1) over(
partition t.no
rowsbetween unbounder preceding and unbounder following
) times, --这一条缴费记录中的号码的缴费次数
max(fee) over(
partition t.no
rowsbetween unbounder preceding and unbounder following
) max_fee, --这一条缴费记录中的号码的最大缴费金额
min(fee) over(
partition t.no
rowsbetween unbounder preceding and unbounder following
) min_fee, --这一条缴费记录中的号码的最小缴费金额
from t
)
select id,no,fee
from aa
where times>1 --缴费次数>1
and max_fee<>min_fee--该记录的最大缴费金额不等于最小缴费金额,换言之总有一个跟本次金额不同
简化为这样:select *
from t
where t.no in( select no
from t
gruop by no
having max(fee)<>min(fee)
)
入过你的no fee有非空约束的联合索引select no fee from的速度是很快的