简体   繁体   中英

ORACLE: How to check for and remove repeating column values

For the "B numbers" - (the number to which a call was made), please limit each occurrence to 3. That is, from the list of "A numbers"-(the number that made the call to the "B number"), we may have multiple persons calling the same "B number". In instances where the "B number" appears more than 3 times in the total dial list, please remove them from the subsequent "A numbers" that they may show up for.

I want to figure out how can i check for and remove these repeating "B numbers" when they are greater than 3 occurrences.

Here is a sample list the table structure.

在此处输入图片说明

So where the B number occurs more than three time i want to keep the A number but remove the B number. Any Thought?

Limiting your results to 3 B Number s at most is easy using the row_number() analytic function.

select a_number, b_number
  from (select a_number, b_number,
               row_number() over (partition by b_number order by null) as rn
          from your_table)
 where rn <= 3

However, the above query is not explicit about which 3 rows it will preserve ( order by null ).

If you want to keep the first 3 occurrences of a B Number in your list, then you need a way to explicitly define the order of your list. Do you have some timestamp field perhaps?

In any case, whatever field(s) define(s) the order of your list, use that in the order by clause of the row_number() function call:

row_number() over (partition by b_number order by pick_an_ordering_column)

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM