Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Database & SysAdmin
  3. Database
  4. Product of Salary

Product of Salary

Scheduled Pinned Locked Moved Database
databasehelpquestioncareer
20 Posts 8 Posters 2 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Jorgen Andersson

    You can use select exp(sum(ln(sal))) from emp which of course will throw an exception for negative numbers and zero. But that you can easily fix with a case expression. The best solution is of course to create a function to do the job.

    List of common misconceptions

    L Offline
    L Offline
    Lost User
    wrote on last edited by
    #9

    Can u please explain with the form of SQL Query.

    If you can think then I Can.

    J B 2 Replies Last reply
    0
    • L Lost User

      Can u please explain with the form of SQL Query.

      If you can think then I Can.

      J Offline
      J Offline
      Jorgen Andersson
      wrote on last edited by
      #10

      eg_Anubhava wrote:

      Can u please explain with the form of SQL Query.

      Well I did. I'll do it again, with extra explanation, and I'll write slowly. You have a table EMP that has a column SAL that you want the product from instead of the sum. Then the query can look like this:

      SELECT EXP(SUM(LN(SAL)))
      FROM EMP

      This query has the drawback that the LN() function will throw an exception for negative numbers and zero. Tip: CASE WHEN and SIGN() will be useful. This can be mathematically written like: A*B = eln(A)+ln(B)

      List of common misconceptions

      L 1 Reply Last reply
      0
      • L Lost User

        Can u please explain with the form of SQL Query.

        If you can think then I Can.

        B Offline
        B Offline
        Bernhard Hiller
        wrote on last edited by
        #11

        eg_Anubhava wrote:

        Can u please explain with the form of SQL Query.

        which proves that you did not look at the link I posted above. It is shown there!

        J 1 Reply Last reply
        0
        • J Jorgen Andersson

          eg_Anubhava wrote:

          Can u please explain with the form of SQL Query.

          Well I did. I'll do it again, with extra explanation, and I'll write slowly. You have a table EMP that has a column SAL that you want the product from instead of the sum. Then the query can look like this:

          SELECT EXP(SUM(LN(SAL)))
          FROM EMP

          This query has the drawback that the LN() function will throw an exception for negative numbers and zero. Tip: CASE WHEN and SIGN() will be useful. This can be mathematically written like: A*B = eln(A)+ln(B)

          List of common misconceptions

          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #12

          It is generating an Error while executing this Query. Error Message is : Undefined Function 'LN' in Expression.

          If you can think then I Can.

          J 1 Reply Last reply
          0
          • L Lost User

            Hello Everybody, As we find the Sum of Salary from Emp Table. SQL Query : Select Sum(Emp) from Emp; Now I want to get the product of Salary : Product means (first sala * Second Sal * Third Salary * ...... * Last salary); How can i use with the help of SQL Command. I Do't want to USE PL/SQL Command. Thanks

            If you can think then I Can.

            P Offline
            P Offline
            Pete OHanlon
            wrote on last edited by
            #13

            If you don't want to use any of the built in Oracle commands, then you could achieve this by using a cursor to iterate over the user's salary and keep a running total that way.

            I'm not a stalker, I just know things. Oh by the way, you're out of milk.

            Forgive your enemies - it messes with their heads

            My blog | My articles | MoXAML PowerToys | Onyx

            1 Reply Last reply
            0
            • L Lost User

              It is generating an Error while executing this Query. Error Message is : Undefined Function 'LN' in Expression.

              If you can think then I Can.

              J Offline
              J Offline
              Jorgen Andersson
              wrote on last edited by
              #14

              ln() is supported on Oracle since version 8i. If you're using an older version than that, you're having bigger problems than the missing ln() function. But if you by any chance aren't using Oracle despite your numerous references to PL/SQL, then both SqlServer and MSAccess uses log() instead of ln().

              List of common misconceptions

              1 Reply Last reply
              0
              • B Bernhard Hiller

                eg_Anubhava wrote:

                Can u please explain with the form of SQL Query.

                which proves that you did not look at the link I posted above. It is shown there!

                J Offline
                J Offline
                Jorgen Andersson
                wrote on last edited by
                #15

                And obviously I didn't either. :doh:

                List of common misconceptions

                1 Reply Last reply
                0
                • L Lost User

                  Hello Everybody, As we find the Sum of Salary from Emp Table. SQL Query : Select Sum(Emp) from Emp; Now I want to get the product of Salary : Product means (first sala * Second Sal * Third Salary * ...... * Last salary); How can i use with the help of SQL Command. I Do't want to USE PL/SQL Command. Thanks

                  If you can think then I Can.

                  R Offline
                  R Offline
                  RyanEK
                  wrote on last edited by
                  #16

                  declare @temp table (ename varchar(10), sal int)
                  insert into @temp values ('Anu', 10)
                  insert into @temp values ('Minu', 20)
                  insert into @temp values ('Tusar', 20)
                  insert into @temp values ('Piter', 10)

                  select exp(sum(log(sal))) from @temp

                  L 1 Reply Last reply
                  0
                  • R RyanEK

                    declare @temp table (ename varchar(10), sal int)
                    insert into @temp values ('Anu', 10)
                    insert into @temp values ('Minu', 20)
                    insert into @temp values ('Tusar', 20)
                    insert into @temp values ('Piter', 10)

                    select exp(sum(log(sal))) from @temp

                    L Offline
                    L Offline
                    Lost User
                    wrote on last edited by
                    #17

                    It returns the wrong answer in some cases change the values and then try. the difference of values are in some points.

                    If you can think then I Can.

                    R 1 Reply Last reply
                    0
                    • L Lost User

                      It returns the wrong answer in some cases change the values and then try. the difference of values are in some points.

                      If you can think then I Can.

                      R Offline
                      R Offline
                      RyanEK
                      wrote on last edited by
                      #18

                      Try

                      SELECT CAST(ROUND(COALESCE(EXP(SUM(LOG(ABS(NULLIF(sal,0))))),0)*SIGN(MIN( ABS(sal)))*(COUNT(NULLIF(SIGN(sal),1))%2*-2+1),0) AS INTEGER)
                      FROM @temp

                      L 1 Reply Last reply
                      0
                      • R RyanEK

                        Try

                        SELECT CAST(ROUND(COALESCE(EXP(SUM(LOG(ABS(NULLIF(sal,0))))),0)*SIGN(MIN( ABS(sal)))*(COUNT(NULLIF(SIGN(sal),1))%2*-2+1),0) AS INTEGER)
                        FROM @temp

                        L Offline
                        L Offline
                        Lost User
                        wrote on last edited by
                        #19

                        Briallent Wow Thanks

                        If you can think then I Can.

                        1 Reply Last reply
                        0
                        • L Lost User

                          Hello Everybody, As we find the Sum of Salary from Emp Table. SQL Query : Select Sum(Emp) from Emp; Now I want to get the product of Salary : Product means (first sala * Second Sal * Third Salary * ...... * Last salary); How can i use with the help of SQL Command. I Do't want to USE PL/SQL Command. Thanks

                          If you can think then I Can.

                          W Offline
                          W Offline
                          Wendelius
                          wrote on last edited by
                          #20

                          Hi, Even though you mentioned that you don't want to use PL/SQL you could be interested in this: Custom aggregates in Oracle[^] Best regards, mika

                          The need to optimize rises from a bad design.My articles[^]

                          1 Reply Last reply
                          0
                          Reply
                          • Reply as topic
                          Log in to reply
                          • Oldest to Newest
                          • Newest to Oldest
                          • Most Votes


                          • Login

                          • Don't have an account? Register

                          • Login or register to search.
                          • First post
                            Last post
                          0
                          • Categories
                          • Recent
                          • Tags
                          • Popular
                          • World
                          • Users
                          • Groups