Topic
IC4NOTICE: developerWorks Community will be offline May 29-30, 2015 while we upgrade to the latest version of IBM Connections. For more information, read our upgrade FAQ.
2 replies Latest Post - ‏2013-06-11T16:13:13Z by Tonkuma
B6MM_Giritharan_Thava
1 Post
ACCEPTED ANSWER

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
    16 Posts
    ACCEPTED ANSWER

    Re: Advance use of the SUM function

    ‏2013-06-10T12:33:17Z  in response to B6MM_Giritharan_Thava

    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
      ACCEPTED ANSWER

      Re: Advance use of the SUM function

      ‏2013-06-11T16:13:13Z  in response to KayKanekowski

      > 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