SQL SUM function
im trying to run this SQL statement:
i keep getting an error message saying: "You tried to execute a query that does not include the specified expression 'MESSAGE_TYPE' as part of an aggregate function"
strSQL = strSQL & "SELECT MESSAGE_TYPE, ORDERS, ZSM_PURCH_INFO, DATE, SUM([ORDERS]) AS TOTALORDERS
WHERE DATE Like '%" & Date2Get & "%'"
when i take out the "SUM([ORDERS]) AS TOTALORDERS", the SQL runs properly. anyone know how to fix this?
thanks in advance.
Don't know about using LIKE on a date although I kept that part from your post above, but as far as the rest goes perhaps add a GROUP BY clause and possibly change WHERE to be HAVING:
Also Date is a reserved word so I wouldn't suggest actually using that as a field name. But if you do, then at least surround it with brackets.
strSQL = strSQL & "SELECT MESSAGE_TYPE, ORDERS, ZSM_PURCH_INFO, [DATE], SUM([ORDERS]) AS TOTALORDERS
GROUP BY MESSAGE_TYPE, ORDERS, ZSM_PURCH_INFO
HAVING [DATE] Like '%" & Date2Get & "%'"
thanks for the reply. it works, however, when i try to put TOTALORDERS in a <TR> to display, it doesnt add the columns up, it only displays the last number in the record.
here's the code where this happens:
how do i fix it so that TOTALORDERS displays the sum of ORDERS, as specified in my SQL from the first post?
Do While Not rs.EOF
ord = rs("ORDERS")
zsm = rs ("ZSM_PURCH_INFO")
<td align="left" valign="top" width="65%"><%= mess %></td>
<td><%= ord %></td>
<td><%= zsm %></td>
<td align="left" class="tabledkblue" valign="top" width="65%">Totals</td>
<td><%= totord %></td>
Set objRecordset = Nothing
Set objConnection = Nothing
thanks again. i appreciate the help.
Here's a little something I wrote to myself awhile back about using subqueries which is something you may want to consider as it sounds like you want to show each detail record along with a total for all records:
Example of one query (QueryB) based on the results of another query (QueryA):
QueryA = "SELECT CustID FROM tblCUSTOMERS WHERE CustName = 'A%'"
QueryB = "SELECT CustID, CustName FROM tblCUSTOMERS WHERE CustID IN (" & QueryA & ")"
But the following is even faster and allows for more than one field to be returned in QueryA:
QueryB = "SELECT tblCUSTOMERS.CustID, CustName FROM (" & strSQLA & ") AS tblSQLA INNER JOIN tblCUSTOMERS ON tblSQLA.CustID = tblCUSTOMERS.CustID"
So QueryA would include all the CustID's for customers starting with A.
And QueryB would include more fields in the customers table (i.e. not just the CustID field) for the records returned in QueryA (which was the customers starting with A).
I suppose it wouldn't hurt to always use LEFT JOIN's in QueryB and build from the tblSQLA on the left to other tables that have fields you want to return.
Users Browsing this Thread
There are currently 1 users browsing this thread. (0 members and 1 guests)