Forums
This topic is locked
SUM function (MySQL & ASP)
Posted 24 Sep 2002 19:26:10
1
has voted
24 Sep 2002 19:26:10 Gary Whittle posted:
Hi.I have a table called "lic_main_table" and a column within called "commission".
The commission contains numeric information.
What I want to do is display the total commission (sum of all records) on an ASP page.
At present I can do this by using hidden text fields within a record count, and using a JS calculation script.
However, I want to know how to do this the proper way.
Any help greatly appreicated.
Gary.
Replies
Replied 25 Sep 2002 11:05:59
25 Sep 2002 11:05:59 Stuart Harland replied:
Well if your pulling data straight from a database you could use a SQL Statement like this:
sSQL = "SELECT Sum(lic_main_table.commission) AS SumOfCommission
FROM lic_main_table"
Then (lets say ur recordset is just called 'rs', then just use: rs("SumOfCommission"
Hope this helps!
********************
wheatNOTmeat
sSQL = "SELECT Sum(lic_main_table.commission) AS SumOfCommission
FROM lic_main_table"
Then (lets say ur recordset is just called 'rs', then just use: rs("SumOfCommission"
Hope this helps!
********************
wheatNOTmeat
Replied 25 Sep 2002 17:54:05
25 Sep 2002 17:54:05 Gary Whittle replied:
Hi again.
That seems to be OK if the only column I need to display is Commission, which is great! Thanks BUT, I need to have all columns in that table present in the recordset.
Like So:
SELECT *, Sum(lic_main_table.commission) AS SumOfCommission
FROM lic_main_table
WHERE timestamp BETWEEN 'MMColParam' and 'MMColParam2' and status_stage = 'MMColParam3'
The problem is, I keep getting an error which says "Mixing of group columns MIN(),MAX(),COUN() with no group columns is Illigal where there is no GROUP BY clause.
I have tried reading about GROUP BY, but have NO idea what it means or how to make it work.
Sorry to be a pain! Any chance you could help again?
Thanks for all the effort so far!
Gary.<img src=../images/dwzone/forum/icon_smile.gif border=0 align=middle>
That seems to be OK if the only column I need to display is Commission, which is great! Thanks BUT, I need to have all columns in that table present in the recordset.
Like So:
SELECT *, Sum(lic_main_table.commission) AS SumOfCommission
FROM lic_main_table
WHERE timestamp BETWEEN 'MMColParam' and 'MMColParam2' and status_stage = 'MMColParam3'
The problem is, I keep getting an error which says "Mixing of group columns MIN(),MAX(),COUN() with no group columns is Illigal where there is no GROUP BY clause.
I have tried reading about GROUP BY, but have NO idea what it means or how to make it work.
Sorry to be a pain! Any chance you could help again?
Thanks for all the effort so far!
Gary.<img src=../images/dwzone/forum/icon_smile.gif border=0 align=middle>
Replied 25 Sep 2002 18:35:05
25 Sep 2002 18:35:05 Gary Whittle replied:
Hi again.
Done some reading on GROUP BY and now all working fine. It explained that group ny splits the table into "smaller groups". I needed to find the commission on different criteria, luckily this was already in the URL filter.
Have a look at the page code: All works fine now.
<%@LANGUAGE="JAVASCRIPT" CODEPAGE="1252"%>
<!--#include file="Connections/licdates.asp" -->
<%
var Recordset1__MMColParam = "1";
if (String(Request.QueryString("startdate") != "undefined" &&
String(Request.QueryString("startdate") != "" {
Recordset1__MMColParam = String(Request.QueryString("startdate");
}
%>
<%
var Recordset1__MMColParam2 = "1";
if (String(Request.QueryString("enddate") != "undefined" &&
String(Request.QueryString("enddate") != "" {
Recordset1__MMColParam2 = String(Request.QueryString("enddate");
}
%>
<%
var Recordset1__MMColParam3 = "1";
if (String(Request.QueryString("status_stage") != "undefined" &&
String(Request.QueryString("status_stage") != "" {
Recordset1__MMColParam3 = String(Request.QueryString("status_stage");
}
%>
<%
var Recordset1 = Server.CreateObject("ADODB.Recordset"
Recordset1.ActiveConnection = MM_licdates_STRING;
startDate = "123";
Recordset1.Source = "SELECT * FROM lic_main_table WHERE timestamp BETWEEN '"+ Recordset1__MMColParam.replace(/'/g, "''" + "' and '"+ Recordset1__MMColParam2.replace(/'/g, "''" + "' and status_stage = '"+ Recordset1__MMColParam3.replace(/'/g, "''" + "'";
Recordset1.CursorType = 0;
Recordset1.CursorLocation = 2;
Recordset1.LockType = 1;
Recordset1.Open();
var Recordset1_numRows = 0;
%>
<%
var sumofcomm__MMColParam4 = "1";
if (String(Request.QueryString("status_stage") != "undefined" &&
String(Request.QueryString("status_stage") != "" {
sumofcomm__MMColParam4 = String(Request.QueryString("status_stage");
}
%>
<%
var sumofcomm = Server.CreateObject("ADODB.Recordset"
sumofcomm.ActiveConnection = MM_licdates_STRING;
sumofcomm.Source = "SELECT Sum(lic_main_table.commission) AS SumOfCommission FROM lic_main_table WHERE status_stage = '"+ sumofcomm__MMColParam4.replace(/'/g, "''" + "' GROUP BY status_stage";
sumofcomm.CursorType = 0;
sumofcomm.CursorLocation = 2;
sumofcomm.LockType = 1;
sumofcomm.Open();
var sumofcomm_numRows = 0;
%>
<%
var Repeat1__numRows = -1;
var Repeat1__index = 0;
Recordset1_numRows += Repeat1__numRows;
%>
<%
// *** Recordset Stats, Move To Record, and Go To Record: declare stats variables
// set the record count
var Recordset1_total = Recordset1.RecordCount;
// set the number of rows displayed on this page
if (Recordset1_numRows < 0) { // if repeat region set to all records
Recordset1_numRows = Recordset1_total;
} else if (Recordset1_numRows == 0) { // if no repeat regions
Recordset1_numRows = 1;
}
// set the first and last displayed record
var Recordset1_first = 1;
var Recordset1_last = Recordset1_first + Recordset1_numRows - 1;
// if we have the correct record count, check the other stats
if (Recordset1_total != -1) {
Recordset1_numRows = Math.min(Recordset1_numRows, Recordset1_total);
Recordset1_first = Math.min(Recordset1_first, Recordset1_total);
Recordset1_last = Math.min(Recordset1_last, Recordset1_total);
}
%>
<%
// *** Recordset Stats: if we don't know the record count, manually count them
if (Recordset1_total == -1) {
// count the total records by iterating through the recordset
for (Recordset1_total=0; !Recordset1.EOF; Recordset1.MoveNext()) {
Recordset1_total++;
}
// reset the cursor to the beginning
if (Recordset1.CursorType > 0) {
if (!Recordset1.BOF) Recordset1.MoveFirst();
} else {
Recordset1.Requery();
}
// set the number of rows displayed on this page
if (Recordset1_numRows < 0 || Recordset1_numRows > Recordset1_total) {
Recordset1_numRows = Recordset1_total;
}
// set the first and last displayed record
Recordset1_last = Math.min(Recordset1_first + Recordset1_numRows - 1, Recordset1_total);
Recordset1_first = Math.min(Recordset1_first, Recordset1_total);
}
%>
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body>
<table width="720" border="0" align="center" cellpadding="0" cellspacing="0">
<tr>
<td><font size="4" face="Verdana, Arial, Helvetica, sans-serif"><%=(Recordset1.Fields.Item("status_stage".Value)%> management report</font></td>
</tr>
</table>
<table width="720" border="0" align="center" cellpadding="0" cellspacing="0">
<tr align="left" valign="middle" bgcolor="#CCCCCC">
<td width="8%" height="0"><font size="1" face="Verdana, Arial, Helvetica, sans-serif">ID</font></td>
<td width="4%" height="0"> </td>
<td width="14%" height="0"><font size="1" face="Verdana, Arial, Helvetica, sans-serif">SURNAME</font></td>
<td width="24%" height="0"><font size="1" face="Verdana, Arial, Helvetica, sans-serif">Insurance
Company </font></td>
<td width="20%" height="0"><font size="1" face="Verdana, Arial, Helvetica, sans-serif">Policy
Number </font></td>
<td width="14%" height="0"><font size="1" face="Verdana, Arial, Helvetica, sans-serif">Date</font></td>
<td width="14%" height="0"><font size="1" face="Verdana, Arial, Helvetica, sans-serif">Commission
Due </font></td>
</tr>
</table>
<% while ((Repeat1__numRows-- != 0) && (!Recordset1.EOF)) { %>
<table width="720" border="0" align="center" cellpadding="0" cellspacing="0">
<tr align="left" valign="top" bgcolor="#FFFFFF">
<td width="8%"><font size="1" face="Verdana, Arial, Helvetica, sans-serif"><%=(Recordset1.Fields.Item("ID".Value)%></font></td>
<td width="4%"><font size="1" face="Verdana, Arial, Helvetica, sans-serif"> </font></td>
<td width="14%"><font size="1" face="Verdana, Arial, Helvetica, sans-serif"><%=(Recordset1.Fields.Item("applicant_1_surname".Value)%> </font></td>
<td width="24%"><font size="1" face="Verdana, Arial, Helvetica, sans-serif"><%=(Recordset1.Fields.Item("provider".Value)%> </font></td>
<td width="20%"><font size="1" face="Verdana, Arial, Helvetica, sans-serif">Coming
soon... </font></td>
<td width="14%"><font size="1" face="Verdana, Arial, Helvetica, sans-serif">Coming
soon... </font></td>
<td width="14%"><font size="1" face="Verdana, Arial, Helvetica, sans-serif">£<%=(Recordset1.Fields.Item("commission".Value)%></font></td>
</tr>
</table>
<%
Repeat1__index++;
Recordset1.MoveNext();
}
%>
<table width="720" border="0" align="center" cellpadding="0" cellspacing="0">
<tr align="left" valign="top" bgcolor="#FFFFFF">
<td width="8%"> </td>
<td width="4%"> </td>
<td width="14%"> </td>
<td width="20%"> </td>
<td width="24%"> </td>
<td width="14%"><strong><font size="1" face="Verdana, Arial, Helvetica, sans-serif">Total:</font></strong></td>
<td width="14%"><font size="1" face="Verdana, Arial, Helvetica, sans-serif"><strong>£<%=(sumofcomm.Fields.Item("SumOfCommission".Value)%></strong></font></td>
</tr>
</table>
</body>
</html>
<%
Recordset1.Close();
%>
<%
sumofcomm.Close();
%>
Done some reading on GROUP BY and now all working fine. It explained that group ny splits the table into "smaller groups". I needed to find the commission on different criteria, luckily this was already in the URL filter.
Have a look at the page code: All works fine now.
<%@LANGUAGE="JAVASCRIPT" CODEPAGE="1252"%>
<!--#include file="Connections/licdates.asp" -->
<%
var Recordset1__MMColParam = "1";
if (String(Request.QueryString("startdate") != "undefined" &&
String(Request.QueryString("startdate") != "" {
Recordset1__MMColParam = String(Request.QueryString("startdate");
}
%>
<%
var Recordset1__MMColParam2 = "1";
if (String(Request.QueryString("enddate") != "undefined" &&
String(Request.QueryString("enddate") != "" {
Recordset1__MMColParam2 = String(Request.QueryString("enddate");
}
%>
<%
var Recordset1__MMColParam3 = "1";
if (String(Request.QueryString("status_stage") != "undefined" &&
String(Request.QueryString("status_stage") != "" {
Recordset1__MMColParam3 = String(Request.QueryString("status_stage");
}
%>
<%
var Recordset1 = Server.CreateObject("ADODB.Recordset"
Recordset1.ActiveConnection = MM_licdates_STRING;
startDate = "123";
Recordset1.Source = "SELECT * FROM lic_main_table WHERE timestamp BETWEEN '"+ Recordset1__MMColParam.replace(/'/g, "''" + "' and '"+ Recordset1__MMColParam2.replace(/'/g, "''" + "' and status_stage = '"+ Recordset1__MMColParam3.replace(/'/g, "''" + "'";
Recordset1.CursorType = 0;
Recordset1.CursorLocation = 2;
Recordset1.LockType = 1;
Recordset1.Open();
var Recordset1_numRows = 0;
%>
<%
var sumofcomm__MMColParam4 = "1";
if (String(Request.QueryString("status_stage") != "undefined" &&
String(Request.QueryString("status_stage") != "" {
sumofcomm__MMColParam4 = String(Request.QueryString("status_stage");
}
%>
<%
var sumofcomm = Server.CreateObject("ADODB.Recordset"
sumofcomm.ActiveConnection = MM_licdates_STRING;
sumofcomm.Source = "SELECT Sum(lic_main_table.commission) AS SumOfCommission FROM lic_main_table WHERE status_stage = '"+ sumofcomm__MMColParam4.replace(/'/g, "''" + "' GROUP BY status_stage";
sumofcomm.CursorType = 0;
sumofcomm.CursorLocation = 2;
sumofcomm.LockType = 1;
sumofcomm.Open();
var sumofcomm_numRows = 0;
%>
<%
var Repeat1__numRows = -1;
var Repeat1__index = 0;
Recordset1_numRows += Repeat1__numRows;
%>
<%
// *** Recordset Stats, Move To Record, and Go To Record: declare stats variables
// set the record count
var Recordset1_total = Recordset1.RecordCount;
// set the number of rows displayed on this page
if (Recordset1_numRows < 0) { // if repeat region set to all records
Recordset1_numRows = Recordset1_total;
} else if (Recordset1_numRows == 0) { // if no repeat regions
Recordset1_numRows = 1;
}
// set the first and last displayed record
var Recordset1_first = 1;
var Recordset1_last = Recordset1_first + Recordset1_numRows - 1;
// if we have the correct record count, check the other stats
if (Recordset1_total != -1) {
Recordset1_numRows = Math.min(Recordset1_numRows, Recordset1_total);
Recordset1_first = Math.min(Recordset1_first, Recordset1_total);
Recordset1_last = Math.min(Recordset1_last, Recordset1_total);
}
%>
<%
// *** Recordset Stats: if we don't know the record count, manually count them
if (Recordset1_total == -1) {
// count the total records by iterating through the recordset
for (Recordset1_total=0; !Recordset1.EOF; Recordset1.MoveNext()) {
Recordset1_total++;
}
// reset the cursor to the beginning
if (Recordset1.CursorType > 0) {
if (!Recordset1.BOF) Recordset1.MoveFirst();
} else {
Recordset1.Requery();
}
// set the number of rows displayed on this page
if (Recordset1_numRows < 0 || Recordset1_numRows > Recordset1_total) {
Recordset1_numRows = Recordset1_total;
}
// set the first and last displayed record
Recordset1_last = Math.min(Recordset1_first + Recordset1_numRows - 1, Recordset1_total);
Recordset1_first = Math.min(Recordset1_first, Recordset1_total);
}
%>
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body>
<table width="720" border="0" align="center" cellpadding="0" cellspacing="0">
<tr>
<td><font size="4" face="Verdana, Arial, Helvetica, sans-serif"><%=(Recordset1.Fields.Item("status_stage".Value)%> management report</font></td>
</tr>
</table>
<table width="720" border="0" align="center" cellpadding="0" cellspacing="0">
<tr align="left" valign="middle" bgcolor="#CCCCCC">
<td width="8%" height="0"><font size="1" face="Verdana, Arial, Helvetica, sans-serif">ID</font></td>
<td width="4%" height="0"> </td>
<td width="14%" height="0"><font size="1" face="Verdana, Arial, Helvetica, sans-serif">SURNAME</font></td>
<td width="24%" height="0"><font size="1" face="Verdana, Arial, Helvetica, sans-serif">Insurance
Company </font></td>
<td width="20%" height="0"><font size="1" face="Verdana, Arial, Helvetica, sans-serif">Policy
Number </font></td>
<td width="14%" height="0"><font size="1" face="Verdana, Arial, Helvetica, sans-serif">Date</font></td>
<td width="14%" height="0"><font size="1" face="Verdana, Arial, Helvetica, sans-serif">Commission
Due </font></td>
</tr>
</table>
<% while ((Repeat1__numRows-- != 0) && (!Recordset1.EOF)) { %>
<table width="720" border="0" align="center" cellpadding="0" cellspacing="0">
<tr align="left" valign="top" bgcolor="#FFFFFF">
<td width="8%"><font size="1" face="Verdana, Arial, Helvetica, sans-serif"><%=(Recordset1.Fields.Item("ID".Value)%></font></td>
<td width="4%"><font size="1" face="Verdana, Arial, Helvetica, sans-serif"> </font></td>
<td width="14%"><font size="1" face="Verdana, Arial, Helvetica, sans-serif"><%=(Recordset1.Fields.Item("applicant_1_surname".Value)%> </font></td>
<td width="24%"><font size="1" face="Verdana, Arial, Helvetica, sans-serif"><%=(Recordset1.Fields.Item("provider".Value)%> </font></td>
<td width="20%"><font size="1" face="Verdana, Arial, Helvetica, sans-serif">Coming
soon... </font></td>
<td width="14%"><font size="1" face="Verdana, Arial, Helvetica, sans-serif">Coming
soon... </font></td>
<td width="14%"><font size="1" face="Verdana, Arial, Helvetica, sans-serif">£<%=(Recordset1.Fields.Item("commission".Value)%></font></td>
</tr>
</table>
<%
Repeat1__index++;
Recordset1.MoveNext();
}
%>
<table width="720" border="0" align="center" cellpadding="0" cellspacing="0">
<tr align="left" valign="top" bgcolor="#FFFFFF">
<td width="8%"> </td>
<td width="4%"> </td>
<td width="14%"> </td>
<td width="20%"> </td>
<td width="24%"> </td>
<td width="14%"><strong><font size="1" face="Verdana, Arial, Helvetica, sans-serif">Total:</font></strong></td>
<td width="14%"><font size="1" face="Verdana, Arial, Helvetica, sans-serif"><strong>£<%=(sumofcomm.Fields.Item("SumOfCommission".Value)%></strong></font></td>
</tr>
</table>
</body>
</html>
<%
Recordset1.Close();
%>
<%
sumofcomm.Close();
%>