Algorithm problem--Help

Ask a Question related to ASP Database, Design and Development.

  1. #1

    Default Re: Algorithm problem--Help

    Try the following for starters. It's untested but should give you the idea.
    It assumes that the records you want to group are already sorted by name -
    i.e. all those starting with the same first letter are together.

    Let us know how you go.

    Alan


    Dim Total
    Dim Count
    Dim CurrentLetter
    Dim LastLetter
    Dim Name
    Dim Owing
    Dim RS

    CurrentLetter = ""
    LastLetter = ""
    Total = 0
    Count = 0

    ' Get your RS here.
    Set RS=GetValues()

    Do While Not RS.EOF

    Name = RS.Fields("Name").Value
    Owing = RS.Fields("Owing").Value
    CurrentLetter = Left (Name, 1)

    If (CurrentLetter <> LastLetter) Then

    If (LastLetter <> "") Then

    ' Display totals for the previous group.
    Average = Total/Count
    Response.write Total & "<br>" & Average

    End If

    ' Initialise the new group.
    Total = 0
    Count = 0
    LastLetter = CurrentLetter

    End If

    Total = Total + Owing
    Count = Count + 1

    RS.MoveNext

    Loop



    "ibsimneej" <ibsimneej@hotmail.com> wrote in message
    news:f4e17b91.0308151334.5563f727@posting.google.c om...
    > Hi I was wondering if you all Guru's can help me out. I ran into an
    > algorithm problem that looks simple but I just cant figure it out.
    >
    > I have a recordset in ASP from a SQL quer at which I want to subtotal
    > some columns as I run down the recordset and I need an algorithm to do
    > it. The recordset is something like this:
    >
    > Name id $Owe
    > John JN 5
    > Judy JN 20
    > Kenny KN 30
    > Kevin KY 40
    > Lon LO 10
    > Lony LO 15
    > Thomas TO 12
    > Tom TO 3
    > Tony TO 45
    > Uel UE 34
    >
    > The recordset is already sorted by the id and or Name.
    > now here is what the result should look like:
    >
    > Name id Owe
    > John JN 5
    > Judy JN 20
    > total = 25 Average = 12.5
    >
    > Kenny KN 30
    > Kevein KY 40
    > total = 70 Average = 35
    >
    > Lon LO 10
    > Lony LO 15
    > total = 25 Average = 12.5
    >
    > Thomas TO 12
    > Tom TO 3
    > Tony TO 45
    > total = 60 Average = 20
    > Uel UE 34
    > Total = 34 Average = 34
    >
    > It looks simple, but I just can't figure it out. Any help would be
    > appreciated.
    >
    > Thanks,
    > IB

    Alan Guest

  2. Similar Questions and Discussions

    1. Which encryption algorithm?
      Hi all, I'm trying to create a web application that will allow Contribute users to change the password themself. Does anyone know which...
    2. /ID tag algorithm
      Does anyone have a program to calculate the /ID tag in a PDF? I know it's optional but I'm generating my own PDFs and want to add one. The PDF...
    3. Algorithm::Huffman
      Hi! I try to use the Algorithm::Huffman to do the compression; however, the compressed file size is larger than the original size. The following is...
    4. Need help speeding up an algorithm...
      I created a pretty simple Perl script to create .m3u files for all genres in my music library. (I eventually want to expand the script to be able...
    5. algorithm help....
      Hello there.... I hope there's someone who can help with one algorithm...I've been trying the whole day.... I'm building my own FORM class and...
  3. #2

    Default Re: Algorithm problem--Help

    You should let the database do this work for you:

    Select [name],id,[Owe$],0 AvgOwed,1 SortBy
    FROM table
    UNION ALL
    Select 'Subtotal',id,sum([$Owe]),avg([$Owe]),2
    FROM table
    Group By id
    UNION ALL
    Select 'Total','ZZ',sum([$Owe]),avg([$Owe]),3
    FROM table
    Order By id,SortBy,[name]


    HTH,
    Bob Barrows

    "ibsimneej" <ibsimneej@hotmail.com> wrote in message
    news:f4e17b91.0308151334.5563f727@posting.google.c om...
    > Hi I was wondering if you all Guru's can help me out. I ran into an
    > algorithm problem that looks simple but I just cant figure it out.
    >
    > I have a recordset in ASP from a SQL quer at which I want to subtotal
    > some columns as I run down the recordset and I need an algorithm to do
    > it. The recordset is something like this:
    >
    > Name id $Owe
    > John JN 5
    > Judy JN 20
    > Kenny KN 30
    > Kevin KY 40
    > Lon LO 10
    > Lony LO 15
    > Thomas TO 12
    > Tom TO 3
    > Tony TO 45
    > Uel UE 34
    >
    > The recordset is already sorted by the id and or Name.
    > now here is what the result should look like:
    >
    > Name id Owe
    > John JN 5
    > Judy JN 20
    > total = 25 Average = 12.5
    >
    > Kenny KN 30
    > Kevein KY 40
    > total = 70 Average = 35
    >
    > Lon LO 10
    > Lony LO 15
    > total = 25 Average = 12.5
    >
    > Thomas TO 12
    > Tom TO 3
    > Tony TO 45
    > total = 60 Average = 20
    > Uel UE 34
    > Total = 34 Average = 34
    >
    > It looks simple, but I just can't figure it out. Any help would be
    > appreciated.
    >
    > Thanks,
    > IB

    Bob Barrows 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