SQL Language (MOSC)

MOSC Banner

Exclude current row from being computed

edited Jan 24, 2011 3:19AM in SQL Language (MOSC) 8 commentsAnswered
  Hi,

Is there an easy way in SQL to exclude the current row from being computed when calculating an aggregate? The SQL ANSI window function has the EXCLUDE CURRENT ROW clause, but SQL Oracle does not.

Here is an example. I have 2 columns, and I want to compute the AVG of col2 partition by col1 excluding the current row:

COL1 COL2 AVG(COL2)
1         2          2
1         1          2,5
1         3          1,5

2         3         1,5
2         2          2
2        1          2,5

3        2          3
3       5          1,5
3       1          3,5

4       1         3,5
4       3         2,5
4       4         2


So for col1 = 1

record 1: avg(col2) = avg(col2) for record 2 and record 3

record 2: avg(col2) =  avg(col2) for record 1 and record 3

record 3: avg(col2) = avg(col2) for record 1 and record 2

And so on for other values of col1.

Howdy, Stranger!

Log In

To view full details, sign in to My Oracle Support Community.

Register

Don't have a My Oracle Support Community account? Click here to get started.

Category Leaderboard

Top contributors this month

New to My Oracle Support Community? Visit our Welcome Center

MOSC Help Center