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. calculate percentage of two column value and store into other column

calculate percentage of two column value and store into other column

Scheduled Pinned Locked Moved Database
databasehelp
5 Posts 3 Posters 0 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.
  • M Offline
    M Offline
    Member 10562086
    wrote on last edited by
    #1

    my query like this

    cte3_persen (per) as
    (select ((cte1.totalcount/cte2.TotaCount)* 100 )
    from cte1,cte2)

    i am creating one new cte table shown above
    from that value '2'coming from cte1.totalcount and value '500' coming from cte2.TotaCount
    i want percentage of those value it should be in 0.00% format
    i want ans of per from cte3_persen table is =0.40%
    please help some one

    S N 2 Replies Last reply
    0
    • M Member 10562086

      my query like this

      cte3_persen (per) as
      (select ((cte1.totalcount/cte2.TotaCount)* 100 )
      from cte1,cte2)

      i am creating one new cte table shown above
      from that value '2'coming from cte1.totalcount and value '500' coming from cte2.TotaCount
      i want percentage of those value it should be in 0.00% format
      i want ans of per from cte3_persen table is =0.40%
      please help some one

      S Offline
      S Offline
      Simon_Whale
      wrote on last edited by
      #2

      What are your data types for the TotalCount variables? are they of Decimal type? if so then have a look at this

      DECLARE @value1 DECIMAL(18,2)
      DECLARE @value2 DECIMAL(18,2)

      SET @value1 = 2
      SET @value2 = 500

      SELECT (@value1 / @value2)*100
      SELECT CAST((@value1 / @value2)*100 AS DECIMAL(18,2))

      Every day, thousands of innocent plants are killed by vegetarians. Help end the violence EAT BACON

      M 1 Reply Last reply
      0
      • S Simon_Whale

        What are your data types for the TotalCount variables? are they of Decimal type? if so then have a look at this

        DECLARE @value1 DECIMAL(18,2)
        DECLARE @value2 DECIMAL(18,2)

        SET @value1 = 2
        SET @value2 = 500

        SELECT (@value1 / @value2)*100
        SELECT CAST((@value1 / @value2)*100 AS DECIMAL(18,2))

        Every day, thousands of innocent plants are killed by vegetarians. Help end the violence EAT BACON

        M Offline
        M Offline
        Member 10562086
        wrote on last edited by
        #3

        totalcount is int type

        S 1 Reply Last reply
        0
        • M Member 10562086

          totalcount is int type

          S Offline
          S Offline
          Simon_Whale
          wrote on last edited by
          #4

          is that the same for TotalCount from the second CTE?

          Every day, thousands of innocent plants are killed by vegetarians. Help end the violence EAT BACON

          1 Reply Last reply
          0
          • M Member 10562086

            my query like this

            cte3_persen (per) as
            (select ((cte1.totalcount/cte2.TotaCount)* 100 )
            from cte1,cte2)

            i am creating one new cte table shown above
            from that value '2'coming from cte1.totalcount and value '500' coming from cte2.TotaCount
            i want percentage of those value it should be in 0.00% format
            i want ans of per from cte3_persen table is =0.40%
            please help some one

            N Offline
            N Offline
            Nicholas Swandel
            wrote on last edited by
            #5

            Sounds like you want to round your result to 2 decimal places before dividing by 100 SELECT (2/500)* 100 AS not_rounded, (round(2/500,2))* 100 AS rounded from dual; returns NOT_ROUNDED ROUNDED ----------- ------- 0.4 0

            Regrads

            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