Skip to Main Content
  • Questions
  • Peculiar syntax error with analytic functions and division operator. Same error does not occur with multiplication operator

Breadcrumb

Question and Answer

Chris Saxon

Thanks for the question, l.

Asked: May 02, 2017 - 10:38 am UTC

Last updated: May 03, 2017 - 9:52 am UTC

Version: 12.1.0.2

Viewed 1000+ times

You Asked

CREATE TABLE lhtest1(
C1 varchar(10 char),
C2 varchar(10 char),
C3 varchar(10 char),
C4 varchar(10 char),
C5 varchar(10 char),
C6 varchar(10 char),
C7 varchar(10 char),
C8 varchar(10 char),
C9 varchar(10 char),
C10  varchar(10 char),
C11   varchar(10 char),
n1 number(9));


WITH distribution
        AS (SELECT
                   C1,
                   C2,
                   C3,
                   C4,
                   C5,
                   C6,
                   C7,
                   C8,
                   C9,
                   round(N1) ,
                   round(N1
                   / SUM (
                        N1)
                     OVER (
                        PARTITION BY C1,C2,C3,C4),2)
                      OSUUS, 
                    round (SUM (
                        N1) 
                     OVER (
                        PARTITION BY C1,C2,C4,C5,C6,C7,C8,C9)  -- change / on following line to *
                        /       
                        SUM (
                        N1)
                     OVER (
                        PARTITION BY C1,C2,C4),2)  
              FROM (  SELECT C1,
                             C2,
                             C3,
                             C4,
                             C5,
                             C6,
                             C7,
                             C8,
                             C9,
                             SUM (N1) N1
                        FROM LHTEST1
                    GROUP BY C1,
                             C2,
                             C3,
                             C4,
                             C5,
                             C6,
                             C7,
                             C8,
                             C9
                      HAVING SUM (N1) > 0))
select* from DISTRIBUTION
order by
C1,
C5,
C7;

Please execute above code.

and Chris said...

This is nothing to do with analytics. You'll get the same problem if you place forward slash on its own line in any statement:

SQL> select 2
  2   /
select 2
       *
ERROR at line 1:
ORA-00923: FROM keyword not found where expected


SQL>  1
  1* select 2
SQL> from dual;
SP2-0042: unknown command "from dual" - rest of line ignored.


You get this because / is the command to execute the command in the buffer:

http://docs.oracle.com/database/122/SQPUG/slash.htm#SQPUG162

To avoid this problem, format your code so there are other characters on the same line as the forward slash:

SQL> select 2 /
  2    1
  3  from dual;

       2/1
----------
         2

SQL>
SQL> select 2
  2    /  1
  3  from dual;

       2/1
----------
         2

Rating

  (1 rating)

Is this answer out of date? If it is, please let us know via a Comment

Comments

Thank You!

anonymous, May 03, 2017 - 9:37 am UTC

Only thing that I can complain is that Your excellent answers make me sometimes feel stupid;) Why didn't I think of this obvious reason ?
Chris Saxon
May 03, 2017 - 9:52 am UTC

No worries, we all overlook the obvious sometimes ;)

More to Explore

Analytics

Analytic SQL got you confused? Check out Connor McDonald's complete video course.