Home » SQL & PL/SQL » SQL & PL/SQL » Analytic Function (11g)
Analytic Function [message #625630] Sun, 12 October 2014 00:28 Go to next message
kiwinz
Messages: 13
Registered: September 2014
Location: WELLINGTON
Junior Member
Hi,

I have a query for using order by in partition by clause for using a MAX,MIN function .
please suggest whether order by makes a difference for using functions like MAX,MIN,AVG,. In my opinion.
It will not make any difference as the calculation will be done based on a particular group irrespective of order by on any column
please suggest if this understanding is wrong ????
Re: Analytic Function [message #625632 is a reply to message #625630] Sun, 12 October 2014 01:12 Go to previous messageGo to next message
Michel Cadot
Messages: 68641
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator

Your understanding is wrong.

Re: Analytic Function [message #625633 is a reply to message #625632] Sun, 12 October 2014 01:21 Go to previous messageGo to next message
kiwinz
Messages: 13
Registered: September 2014
Location: WELLINGTON
Junior Member
fine ... i got it Michel..
Re: Analytic Function [message #625634 is a reply to message #625633] Sun, 12 October 2014 01:39 Go to previous messageGo to next message
Michel Cadot
Messages: 68641
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator

What did you get?

And you could also feedback in your previous topics, we don't know if we helped and we don't know if they are over and you could also thank people who take time for you.

Re: Analytic Function [message #625635 is a reply to message #625634] Sun, 12 October 2014 01:54 Go to previous messageGo to next message
kiwinz
Messages: 13
Registered: September 2014
Location: WELLINGTON
Junior Member
well I got that the operation is performed on the current row so order by can produce different results
sure i will do that
Re: Analytic Function [message #625641 is a reply to message #625635] Sun, 12 October 2014 02:38 Go to previous message
Michel Cadot
Messages: 68641
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator

Analytic functions, without any windowing clause, is a rolling function with default window clause as RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW which mean it applies the analytic function to the rows before the first one (in the order you specify) and all the rows having the same value than the current row.
So the order matter. For instance:
SQL> select empno, sum(sal) over(order by sal) sumsalsal, sum(sal) over(order by empno) sumsalemp 
  2  from emp order by empno;
     EMPNO  SUMSALSAL  SUMSALEMP
---------- ---------- ----------
      7369        800        800
      7499       9750       2400
      7521       5350       3650
      7566      18025       6625
      7654       5350       7875
      7698      15050      10725
      7782      12200      13175
      7788      24025      16175
      7839      29025      21175
      7844       8150      22675
      7876       2850      23775
      7900       1750      24725
      7902      24025      27725
      7934       6650      29025

If you omit ORDER BY clause than the function is applied to the whole partition:
SQL> select deptno, empno, 
  2         sum(sal) over(partition by deptno) sumsaldept,
  3         sum(sal) over() sumsalall
  4  from emp
  5  order by deptno, empno
  6  /
    DEPTNO      EMPNO SUMSALDEPT  SUMSALALL
---------- ---------- ---------- ----------
        10       7782       8750      29025
        10       7839       8750      29025
        10       7934       8750      29025
        20       7369      10875      29025
        20       7566      10875      29025
        20       7788      10875      29025
        20       7876      10875      29025
        20       7902      10875      29025
        30       7499       9400      29025
        30       7521       9400      29025
        30       7654       9400      29025
        30       7698       9400      29025
        30       7844       9400      29025
        30       7900       9400      29025

Previous Topic: ORDER BY 'constant' oracle partition
Next Topic: CASE STATEMENT IN DDL
Goto Forum:
  


Current Time: Fri Apr 19 08:36:58 CDT 2024