+ Reply to Thread + Post New Thread
Results 1 to 5 of 5
  1. #1
    sowmyav's Avatar
    sowmyav is offline Junior Member
    Join Date
    11 Dec 2008
    Posts
    8
    Say Thanks
    0
    Thanked 0 Times in 0 Posts
    Documents
    0
    Uploads
    0

    Default Aggregate function in SQL Server

    Hi,

    Please find the below query in sql server.I need to get the sum of the field.The below qry is working fine.
    Code :
    select (arr.PROD),
    arr.Calendar_Month_N,arr.fld_inv,
    (arr.Fld_Inv/sum(arr.Fld_Inv)) as cum_fld,
    ((arr.Fld_Inv/sum(arr.Fld_Inv))*(arr.ARR_3MRA))as fldinv_calc,
     arr.ARR_3MRA
    from DRV_Data_200811 arr
     JOIN Test_Smry_200811 NTF
    ON (ARR.Prod=NTF.Prod And ARR.Calendar_Month=NTF.Calendar_Month)
    group by ARR.PRODUCT,arr.Calendar_Month_N,arr.ARR_3MRA,arr.fld_inv

    ************************************

    But I need to get one more summation which is

    (sum((arr.Fld_Inv/sum(arr.Fld_Inv))*(arr.ARR_3MRA))) as glob in the select field.

    but I am getting error as
    Code :
     "Msg 130, Level 15, State 1, Line 1
    Cannot perform an aggregate function on an expression
    containing an aggregate or a subquery."
    Could any one please help me on the same

  2.    Club-Oracle Complementary E-Books and Magazines
    Get your free Complementary Copy of Oracle Magazine

    You can also browse the Free Magazines and E-Books section to see the complete list of free magazines, e-books and Whitepapers.

  3. #2
    rajavu's Avatar
    rajavu is offline Forum Genius
    Join Date
    13 Oct 2008
    Location
    @ Bangalore , India
    Posts
    471
    Say Thanks
    0
    Thanked 20 Times in 19 Posts
    Documents
    0
    Uploads
    0

    Default Re: Aggregate function

    Hi Sowmya,

    Neither this is SQL SERVER forum , Nor I am Sql Server expert.
    Anyway we can reproduce this error in Oracle also.

    See the following examples.

    Code :
    SQL> SELECT DEPTNO , COMM ,COMM/SUM(SAL) X
      2  FROM EMP2
      3  GROUP BY DEPTNO , COMM;

        DEPTNO       COMM          X
    ---------- ---------- ----------
            20        450      .5625
            30        500 .181818182
            30        700 .318181818
            10        300 .047619048
            20        400 .056537102
            30        300  .06741573
            10        500 .204081633
            20        100 .033333333

    8 rows selected.

    SQL> SELECT DEPTNO ,COMM , SUM(COMM/SUM(SAL)) X
      2  FROM EMP2
      3  GROUP BY DEPTNO , COMM;
    SELECT DEPTNO ,COMM , SUM(COMM/SUM(SAL)) X
           *
    ERROR at line 1:
    ORA-00937: not a single-group group function

    Now we can sum the x again using an outer sub query as below .

    Code :
    SQL> SELECT DEPTNO, SUM(X) X
      2  FROM
      3  (SELECT DEPTNO , COMM ,COMM/SUM(SAL)X
      4  FROM EMP2
      5  GROUP BY DEPTNO , COMM )
      6  GROUP BY DEPTNO ;

        DEPTNO          X
    ---------- ----------
            30  .56741573
            20 .652370436
            10  .25170068

    SQL>

    You can do the same in SQL Server .i guess.

    I dont know whether Cub-oracle support non-oracle related posts.

    Anyway , your are lucky to get the reply .

    Raj.

  4. #3
    Sadik's Avatar
    Sadik is offline Administrator
    Join Date
    03 Aug 2008
    Location
    Kolkata, India
    Posts
    1,184
    Say Thanks
    7
    Thanked 83 Times in 54 Posts
    Documents
    4
    Uploads
    2

    Default Re: Aggregate function in SQL Server

    Moved this to the "Other Databases" Section!
    Learn Oracle with Oracle forum. Check out The Technology Blog

  5. #4
    rajavu's Avatar
    rajavu is offline Forum Genius
    Join Date
    13 Oct 2008
    Location
    @ Bangalore , India
    Posts
    471
    Say Thanks
    0
    Thanked 20 Times in 19 Posts
    Documents
    0
    Uploads
    0

    Default Re: Aggregate function in SQL Server

    Hmhm.. that is the perfect section for this post.

    Raj.

  6. #5
    sowmyav's Avatar
    sowmyav is offline Junior Member
    Join Date
    11 Dec 2008
    Posts
    8
    Say Thanks
    0
    Thanked 0 Times in 0 Posts
    Documents
    0
    Uploads
    0

    Default Re: Aggregate function in SQL Server

    Thank you so much for your valuable reply.

Similar Threads

  1. Replies: 6
    Last Post: 03-23-2010, 06:48 PM
  2. Connection Server : Launch Archiver for Content Server not working
    By noel in forum Oracle Fusion Middleware
    Replies: 1
    Last Post: 02-26-2009, 06:24 AM
  3. How to pass parameters to a pl/sql function from Oracle Discoverer Desktop?
    By Gabo86 in forum Oracle Developer Suite and Tools
    Replies: 3
    Last Post: 02-24-2009, 09:00 AM
  4. DATEDIFF function in Oracle like SQL Server
    By simona007 in forum SQL PL/SQL
    Replies: 2
    Last Post: 02-23-2009, 03:09 AM
  5. PL/SQL function to return more than one value
    By oracle_new in forum SQL PL/SQL
    Replies: 2
    Last Post: 01-28-2009, 11:46 AM

Tags for this Thread