Getting a Total Count of integers within fields

Results 1 to 3 of 3

Thread: Getting a Total Count of integers within fields

  1. #1
    Join Date
    Dec 1969

    Default Getting a Total Count of integers within fields

    What I am doing is pulling in alot of data and I want to know the total value of a certain field for all the records I pull in, because I am doing percentages. The slow, but working method I have right now is 1st just stepping through each record and adding the numbers together. Then I to back to the beginning and display my data. Is there a way I can get a total count of integers with a field other then the above method?

  2. #2
    Join Date
    Dec 1969

    Default RE: Getting a Total Count of integers within field

    Select sum(myIntField) as TheSum from myTable<BR><BR>response.write rs("thesum")

  3. #3
    Join Date
    Dec 1969

    Default That's one answer...

    ...but don&#039;t forget that means you have to do TWO queries of the to get the total, first, and then one to get the records.<BR><BR>You *might* be better off with a variation of what you are doing now: Get all the records, run through them to get the total, then display them.<BR><BR>But the "trick" to making this efficient--and likely even more efficient than simply displaying the records, alone, as you are doing it now--is to convert the entire recordset to an array!<BR><BR>Then it&#039;s easy to do a FOR...NEXT loop to get the total. And another FOR...NEXT loop to display the info. Presto.<BR><BR>&#060;%<BR>...<BR>Set RS = ... get the recordset with data ...<BR><BR>&#039; convert recordset to a 2D array<BR>records = RS.GetRows<BR>&#039; and you are done with the recordet!<BR>RS.Close<BR><BR>maxRowNum = UBound( records, 2 ) &#039; rows go from zero to this<BR><BR>total = 0 &#039; initialize<BR><BR>For row = 0 To maxRowNum <BR>&nbsp; &nbsp; total = total + records( columnOfInterest, row )<BR>Next<BR><BR>&#039; now do it again, for display...<BR>For row = 0 to maxRowNum<BR>&nbsp; &nbsp; Response.Write "&#060;TD&#062;" & records(columnNum, row) & "&#060;/TD&#062;"<BR>&nbsp; &nbsp; ...<BR>&nbsp; &nbsp; &#039; to get the percentage:<BR>&nbsp; &nbsp; Response.Write "&#060;TD&#062;" & FormatPercent( records( columnOfInterest) / total ) & "&#060;/TD&#062;"<BR>&nbsp; &nbsp; ...<BR>Next<BR>...<BR>%&#062;<BR>

Posting Permissions

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