how to sum a database column in asp

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • fran7
    New Member
    • Jul 2006
    • 229

    how to sum a database column in asp

    hi, Anyone can point me in the direction of a way to sum a database colum of numeric values.

    I have table displayed with individual hit counts for all my links and I want to add all the hits to see the total and then post the result back to a webpage.

    I have no working code except this that displays the results. I basically need to sum the total hits column.

    Code:
    Response.Write "<TABLE BORDER=""1"">" & vbCrLf
    Response.Write "<TR>" & vbCrLf
    Response.Write "<TD><B>ID</B></TD>" & vbCrLf
    Response.Write "<TD><B>Name</B></TD>" & vbCrLf
    Response.Write "<TD><B>Location</B></TD>" & vbCrLf
    Response.Write "<TD><B>Total Hits</B></TD>" & vbCrLf
    Response.Write "</TR>" & vbCrLf
    
    Do While Not rsLinkTracker.EOF
    
    	Response.Write "<TR>" & vbCrLf
    	Response.Write "<TD><A HREF=""details.asp?lid=" & rsLinkTracker.Fields("PostCardID").Value & """>" & rsLinkTracker.Fields("PostCardID").Value & "</A></TD>" & vbCrLf
    	Response.Write "<TD><A HREF=""details.asp?lid=" & rsLinkTracker.Fields("PostCardID").Value & """>" & rsLinkTracker.Fields("PostCardID").Value & "</A></TD>" & vbCrLf
    	Response.Write "<TD>" & rsLinkTracker.Fields("DefaultHeadline").Value & "</TD>" & vbCrLf
    	Response.Write "<TD>" & rsLinkTracker.Fields("totalhits").Value & "</TD>" & vbCrLf
    	Response.Write "</TR>" & vbCrLf
    
    	rsLinkTracker.MoveNext
    Loop
    
    Response.Write "</TABLE>" & vbCrLf

    Any wee pointers would be a great help.
    Thanks in advance.
    Richard
  • markrawlingson
    Recognized Expert Contributor
    • Aug 2007
    • 346

    #2
    You basically answered your own question, without realizing it.. Just open a recordset to your table and use the SQL SUM() function to return the sum of all the data in the fields.

    [code=asp]
    sSQL = "SELECT SUM(totalHits) AS iTotalHits FROM tblHits GROUP BY totalHits;"
    Set oRsTemp = Server.CreateOb ject("ADODB.Rec ordSet")
    oRsTemp.Open sSQL, Connection, adoConst, adoConst
    iTotalHits = CInt(oRsTemp("i TotalHits"))
    oRsTemp.Close
    Set oRsTemp = Nothing
    Response.Write iTotalHits
    [/code]

    Sincerely,
    Mark

    Comment

    • fran7
      New Member
      • Jul 2006
      • 229

      #3
      Thanks Mark

      Richard

      Comment

      Working...