Solved

Executing a SELECT (..) GROUP BY (...) statement in ASP

Posted on 2009-04-13
4
385 Views
Last Modified: 2012-08-13
I know this question is a bit daft, but humor me - I'm a bit of a novice when it comes to ASP and SQL.

I'm trying to do a SUM() and GROUP BY statement and display it using ASP. Say I have the following table

O_Id ---- OrderDate ---- OrderPrice ---- Customer
1 --------- 2008/11/12 --- 1000 ---------- Hansen
2 --------- 2008/10/23 --- 1600 ---------- Nilsen
3 --------- 2008/09/02 --- 700 ------------ Hansen
4 --------- 2008/09/03 --- 300 ------------ Hansen
5 --------- 2008/08/30 --- 2000 ---------- Jensen
6 --------- 2008/10/04 --- 100 ------------ Nilsen

And I execute the following SQL statement

"SELECT Customer,SUM(OrderPrice) FROM Orders GROUP BY Customer"

I know that the result should look like this

Customer ---- SUM(OrderPrice)
Hansen ------- 2000
Nilsen --------- 1700
Jensen -------- 2000

But how does the ASP code that created this actually look like?

The example above is from http://www.w3schools.com/sql/sql_groupby.asp btw - A great introduction to SQL GROUP BY, but unless I have some sample code where it is actually used in ASP I'm affraid I don't know how to actually use it....

Any help would be appreciated.
0
Comment
Question by:BestAviation
  • 3
4 Comments
 
LVL 35

Expert Comment

by:YZlat
ID: 24130152
you posted your question in the wrong area. Should have tried ASP
<%
''connect the database
strConn = "Provider=SQLOLEDB;Data Source=YOURSERVERNAME;Initial Catalog=YOURDBNAME;User ID=youruser;Password=yourpwd;"
objConn = Server.CreateObject("ADODB.Connection")
objConn.Open(strConn)


SET rs = objConn.Execute("SELECT Customer,SUM(OrderPrice) FROM Orders GROUP BY Customer")
%>
0
 

Author Comment

by:BestAviation
ID: 24130309
I thought I did that...

That is how far I get as well - to the execution of the SQL. It's the bit that comes after I'm unsure about...

What I'm asking is how do I display the html table below using ASP.

Customer ---- SUM(OrderPrice)
Hansen ------- 2000
Nilsen --------- 1700
Jensen -------- 2000
0
 
LVL 35

Expert Comment

by:YZlat
ID: 24130454
loop through records in a recordset
0
 
LVL 35

Accepted Solution

by:
YZlat earned 250 total points
ID: 24130498
you can display them in a table format:

Dim fld, i

  Response.Write "<table border=1>"
Response.Write "<tr>"
 
  Response.Write "<td>Customer</td>"
   Response.Write "<td>SUM(OrderPrice)</td>"
 
  Response.Write "</tr>"
For i=1 To rs.RecordCount

   If Not rs.EOF Then
    Response.Write "<tr>"
    For Each fld in rs.Fields
     Response.Write "<td>" & fld.Value & "</td>"
    Next
    Response.Write "</tr>"
   rs.MoveNext
   End If
Next

  Response.Write "</table>"

0

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

by Mark Wills Attending one of Rob Farley's seminars the other day, I heard the phrase "The Accidental DBA" and fell in love with it. It got me thinking about the plight of the newcomer to SQL Server...  So if you are the accidental DBA, or, simp…
Hello, all! I just recently started using Microsoft's IIS 7.5 within Windows 7, as I just downloaded and installed the 90 day trial of Windows 7. (Got to love Microsoft for allowing 90 days) The main reason for downloading and testing Windows 7 is t…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now