Topic
  • 2 replies
  • Latest Post - ‏2013-06-11T16:13:13Z by Tonkuma
B6MM_Giritharan_Thava
1 Post

Pinned topic Advance use of the SUM function

‏2013-06-10T11:43:44Z |

As we know, the SUM aggregate function is often used with the GROUP BY clause.

A typical use of SUM is:  SUM(distinct salary).

In the above usecase, only the distinct salary values will be summed. 

 

I would like to know if I can do a SUM on a particular column and the DISTINCT on another column. 

Something like: SUM(DISTINCT empcode salary)

I cannot do this in the SQL-World, but is it possible to achieve this using some in-built DB2 functions.

Thanks in advance.

 

  • KayKanekowski
    KayKanekowski
    17 Posts

    Re: Advance use of the SUM function

    ‏2013-06-10T12:33:17Z  

    Hi,
    what will be the result of your select ? Sorry i don't understand what you want to calculate.
    Perhaps a simple

    select sum(salary) , sum (distinct empcode ), count (distinct empcode ) from ...

    do it ?

    regards
    Kay

  • Tonkuma
    Tonkuma
    4 Posts

    Re: Advance use of the SUM function

    ‏2013-06-11T16:13:13Z  

    Hi,
    what will be the result of your select ? Sorry i don't understand what you want to calculate.
    Perhaps a simple

    select sum(salary) , sum (distinct empcode ), count (distinct empcode ) from ...

    do it ?

    regards
    Kay

    > what will be the result of your select ?

    I also want to ask the question.
     

    Anyway,
    I guessed a possible result, like this.

    SELECT SUM(salary)
     FROM  (SELECT DISTINCT
                   empcode
                 , salary
             FROM  your-table
           ) s