I have rows in Oracle database table (having +2M records) and I want to extract records with some condition. The constraint is not setup on the table. Any help will be appreciated.
nif num_sum
--------------------------
123 456
123 456
123 789 // => I want to select this
134 600
Records having a nif
with one or more same num_sum
is allowed. But if a nif
exists for two or more different num_sum
s, I want to select that nif
and num_sum
. I showed the above records for explanation only.
I tried:
select distinct nif, num_sum
from sumcon
group by nif, num_sum
having count(distinct nif) > 1
您不返回任何记录,因为nif
只有1个不同的值,因此计数不大于1。
Given your description,
"Records having a nif with one or more same num_sum is allowed (a nif shouldn't be for two different num_sums)"
I think you want the following:
SELECT nif
FROM sumcon
GROUP BY nif
HAVING COUNT(DISTINCT num_sum) > 1;
First, in your original query DISTINCT
is superfluous. Next, since you say there should not exist more than one distinct value of num_sum
for a given value of nif
, I think you want to group by nif
and use COUNT(DISTINCT num_sum)
. Now if you mean that a given value of num_sum
should not exist for more than one unique value of nif
, then you would want the following:
SELECT num_sum
FROM sumcon
GROUP BY num_sum
HAVING COUNT(DISTINCT nif) > 1;
If you need to get all the values of nif
for which this is the case, then you would have to do something like this:
SELECT nif, num_sum FROM sumcon
WHERE num_sum IN (
SELECT num_sum
FROM sumcon
GROUP BY num_sum
HAVING COUNT(DISTINCT nif) > 1
);
Hope this helps.
不确定是否正是您要的内容,但我希望它能对您有所帮助。
select nif, num_sum from sumcon group by nif, num_sum having count(1) > 1
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.