ibmi-brunch-learn

Announcement

Collapse
No announcement yet.

Interactive SQL with division

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Interactive SQL with division

    Having problems with the below SQL. Running from an interactive session until it is working correctly but it currently is not returning the percent. Currently it is only return Zero's. Does anyone know what I am missing?

    Code:
    select decimal(percent,10,10), max, num from                     
        (select Decimal(num/max, 10,10) as percent, max, num         
              from (select sum(maxdoc) as max,                       
                    sum(numdoc) as num                               
                    from y07u999s)                                   
         as fold1)                                                   
    as percent
    Return values
    DECIMAL MAX NUM
    .0000000000 14,000,000 8,147,769

  • #2
    I think this is the most complex SQL statement which I met!
    Give up!

    Comment


    • #3
      Wow that is a huge one

      All I can see is that if you divide num by max you get
      .5819835 and you have your field defined as 10.

      so WITHOUT multiplying by 100 to get 58%

      the field would be all *Zeros


      Not sure what the code would look like .....
      Hope that helps maybe try some form of this

      select Decimal((num/max*100), 4) as percent


      Please post your solution when you get it

      thanks
      Jamie
      All my answers were extracted from the "Big Dummy's Guide to the As400"
      and I take no responsibility for any of them.

      www.code400.com

      Comment


      • #4
        Re: Interactive SQL with division

        I know this table, it's the folder balencing table from AWD's SMC databse.

        I see what you trying to do and its this...

        Code:
        select (sum(numdoc) / sum(maxdoc) * 100) as Percent,
                sum(maxdoc) as Max,
                sum(numdoc) as Num
        from y07u999s

        I change our active folder every day with a batch job fldnam='volume' subdir='yyyy\mm\dd' and set maxdoc to all the 9's that way you'll never run out and it makes storage utilization stats easier because yous file system report is also a daily report on disk usage !

        Comment


        • #5
          Re: Interactive SQL with division

          The above works on Oracle and MySQL but for DB2 you need the following (two decimal places).

          Code:
          select substr( char(sum(numdoc)/sum(float(maxdoc))*100),          
                         1,                                                 
                         locate('.',                                        
                                char(sum(numdoc)/sum(float(maxdoc))*100))+2 
                               ),                                           
          sum(maxdoc) as Max,                                               
          sum(numdoc) as Num                                                
          from y07u999s
          Last edited by andy; July 4, 2005, 09:51 AM.

          Comment


          • #6
            Re: Interactive SQL with division

            Isn't "Max" an SQL function? Won't you get into some trouble using that as a field name?
            "Time passes, but sometimes it beats the <crap> out of you as it goes."

            Comment


            • #7
              Re: Interactive SQL with division

              It is a function but a value following an "AS" is used as a literal to name or rename the column in the result set, even duplicates are permitted, ie.

              select col1 as acolumn, col2 as acolumn
              from atable

              is ok too.

              even this works...

              select col1 as SELECT, col2 as FROM, col3 as WHERE ,
              col4 as AND, col5 as HAVING, col6 as CURRENT
              from table1

              .. any reserved word is ok as long as you use the "AS"

              Comment


              • #8
                Re: Interactive SQL with division

                Andy,

                Thanks for clearing that up for me.

                Michael
                "Time passes, but sometimes it beats the <crap> out of you as it goes."

                Comment

                Working...
                X