Calculate Running Average

Ask a Question related to IBM DB2, Design and Development.

  1. #1

    Default Calculate Running Average

    Hi everybody.I'm Facing a ploblem to calculate running average.I am
    explaining the scenario.
    In my table there are two column only
    1.Name
    2.Marks.
    In my query what to show another column with running average.

    Example
    -------
    My table already contains

    Col1 Col2
    ---- -----
    a 8
    b 6
    c 10
    d 12
    e 20

    In my query output I want to show the data like this format

    Col1 Col2 Col3
    ---- ----- ----
    a 8 8 (Average of 1st row) means 10/1
    b 6 7 (Average of 1st + 2nd rows) means (8+6)/2
    c 10 8 (Average of 1st + 2nd + 3rd rows) means (8+6+10)/3
    d 12 9 (Average of 1st + 2nd + 3rd + 4th rows) means
    (8+6+10+12)/4
    e 20 11.2 (Average of 1st + 2nd + 3rd + 4th + 4th rows) means

    (8+6+10+12+20)/5

    I hope it will be clear to all of you.If any problem in understanding
    please let me inform.
    Thanks
    Arijit Chatterjee
    Arijit Chatterjee Guest

  2. Similar Questions and Discussions

    1. average days
      I have a db with two colums create_date and finish_date. What I would like to do is take the datediff of each row , add them togehter, then divide...
    2. Calculating a moving average
      Hi, I need to calculate a moving average and I would like to do it with SQL, or a Pg function built for this purpose. I'm on Pg 7.4. Is this...
    3. average using lingo
      i need a huge hand with something i need to write a function to calculate the average of a sequence and return the result. however it should only...
    4. Average Invoice value problem
      Hi Jason Make calc_RoundTotal a conditional statement that only shows the round total if the record is not a credit note. Bridget Eley
    5. Average MB/s read from snapshot
      All, I've been monitoring the tablespace snapshot information and been calculating how much MB/s the database reads / tablespace (asynch and...
  3. #2

    Default Re: Calculate Running Average

    On 2 Jul 2003, Arijit Chatterjee wrote:
    > Hi everybody.I'm Facing a ploblem to calculate running average.I am
    > explaining the scenario.
    > In my table there are two column only
    > 1.Name
    > 2.Marks.
    > In my query what to show another column with running average.
    >
    > Example
    > -------
    > My table already contains
    >
    > Col1 Col2
    > ---- -----
    > a 8
    > b 6
    > c 10
    > d 12
    > e 20
    >
    > In my query output I want to show the data like this format
    >
    > Col1 Col2 Col3
    > ---- ----- ----
    > a 8 8 (Average of 1st row) means 10/1
    > b 6 7 (Average of 1st + 2nd rows) means (8+6)/2
    > c 10 8 (Average of 1st + 2nd + 3rd rows) means (8+6+10)/3
    > d 12 9 (Average of 1st + 2nd + 3rd + 4th rows) means
    > (8+6+10+12)/4
    > e 20 11.2 (Average of 1st + 2nd + 3rd + 4th + 4th rows) means
    >
    > (8+6+10+12+20)/5
    >
    You can use windowing functions:

    select col1, col2, avg(col2) over (order by col1)


    Good luck,





    -----= Posted via Newsfeeds.Com, Uncensored Usenet News =-----
    [url]http://www.newsfeeds.com[/url] - The #1 Newsgroup Service in the World!
    -----== Over 80,000 Newsgroups - 16 Different Servers! =-----
    Ian D. Bjorhovde Guest

Posting Permissions

  • You may not post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139