Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Count Property-Multiple Values in Same Field Not Working 1

Status
Not open for further replies.

alljax

ISP
Nov 24, 2000
10
US
I need to count different values from the same field and have them print out four separate counts. I also need to be able to sort these by Site ID. Here is how my data looks:

X SiteID
---------------- ---------------------
3 35012
3 12345
3 35012
1 35012
1 35012
3 35012
2 35012
2 12345

So for example, I need to be able to count all the three’s, two’s, and one’s WHERE ((Tbl.SiteID)= '35012') and display the count results individually

Example
Report for Site ID “35012”

Value 3 = 3 Records
Value 2 = 1 Record
Value 1 = 2 Records

This is the code we are using that isn’t working :

sql = "SELECT COUNT(DECODE(x,1,1,NULL)) count_of_1, COUNT(DECODE(x,2,1,NULL)) count_of_2, COUNT(DECODE(x,3,1,NULL)) count_of_3 FROM FinLog.AppSta WHERE ((Tbl.SiteID)= '35012')"


PLEASE HELP!!!!
 
Try this:

SELECT Table.x, Count(Table.SiteID) AS Records
FROM Table
GROUP BY Table.x, Table.SiteID
HAVING (((Table.SiteID)=35012))
ORDER BY Table.x DESC


Simon
 
Simon,
Thanks for the help, unfortunately its not working for me. I think that maybe there is something wrong with my script. Here is my entire script. I am pulling from a database named “ONLAPPCYC2K” and the table is “FinLog”.

~ Chris




<html>
<head>
<meta HTTP-EQUIV=&quot;Content-Type&quot; CONTENT=&quot;text/html;charset=windows-1252&quot;>
<title>test</title>
</head>
<body>
<body BGCOLOR=&quot;FFFFFF&quot;>

<%
Param = Request.QueryString(&quot;Param&quot;)
Data = Request.QueryString(&quot;Data&quot;)
%>
<%

Set conn = Server.CreateObject(&quot;ADODB.Connection&quot;)
conn.open &quot;ONLAPPCYC2K&quot;,&quot;&quot;,&quot;&quot;
Set Session(&quot;ONLAPPCYC2K_conn&quot;) = conn

%>
<%
sql = Request (&quot;sql&quot;)
if sql = &quot;&quot; then
sql = &quot;SELECT FinLog.AppSta, Count(FinLog.SitID) AS Records FROM FinLog GROUP FinLog.AppSta, FinLog.SitID HAVING (((FinLog.SiteID)=CY2KFC)) ORDER BY FinLog.AppSta DESC&quot;


If cstr(Param) <> &quot;&quot; And cstr(Data) <> &quot;&quot; Then
sql = sql & &quot; And [&quot; & cstr(Param) & &quot;] = &quot; & cstr(Data)
End If
End If
Set rs = Server.CreateObject(&quot;ADODB.Recordset&quot;)
rs.Open sql, conn, 3, 3
%>

<CENTER>
<table BORDER=&quot;1&quot;CELLSPACING=&quot;0&quot;>


<tr>
<th BGCOLOR=&quot;Beige&quot; BORDERCOLOR=&quot;#000000&quot;><font SIZE=&quot;2&quot; FACE=&quot;Arial&quot; COLOR=&quot;Black&quot;>Please Work</font></th>

</tr>
</thead>
<tbody>

<%Do while not RS.EOF%>

<%
On Error Resume Next%>
<!--rs.MoveFirst do while Not rs.eof %-->
<tr VALIGN=&quot;TOP&quot;>
<td BORDERCOLOR=&quot;#c0c0c0&quot;><font SIZE=&quot;-1&quot; FACE=&quot;Arial&quot; COLOR=&quot;#000000&quot;><%=Server.HTMLEncode(rs.Fields(&quot;AppSta&quot;).Value)%><br></font></td>

</tr>
<%
On Error Resume Next
rs.MoveNext
loop
%>


<%set Conn = nothing%>



</tbody>
<tfoot></tfoot>
</table>
</CENTER>


</body>
</html>

 
I can see two things wrong without running the code:

1. in the sql statement you have &quot;... GROUP FinLog.AppSta,...&quot; but that should be &quot;... GROUP BY FinLog.AppSta,...&quot;

2. The field SiteID looks like it is a text field, by the WHERE clause you are using, so you should have quotes around CY2KFC in the WHERE clause.

Simon
 
I still get this error:

Error Type:
Microsoft VBScript compilation (0x800A0401)
Expected end of statement
/please.asp, line 23, column 135
sql = &quot;SELECT FinLog.AppSta, Count(FinLog.SitID) AS Records FROM FinLog GROUP BY FinLog.AppSta, FinLog.SitID HAVING (((FinLog.SiteID)=&quot;CY2KFC&quot;)) ORDER BY FinLog.AppSta DESC&quot;
--------------------------------------------------------------------------------------------------------------------------------------^

~ Chris

 
Try this:

sql = &quot;SELECT FinLog.AppSta, Count(FinLog.SitID) AS Records FROM FinLog GROUP BY FinLog.AppSta, FinLog.SitID HAVING (((FinLog.SiteID)='CY2KFC')) ORDER BY FinLog.AppSta DESC&quot;

OR try this:

sql = &quot;SELECT FinLog.AppSta, Count(FinLog.SitID) AS Records FROM FinLog GROUP BY FinLog.AppSta, FinLog.SitID HAVING (((FinLog.SiteID)=&quot; & chr(34) & &quot;CY2KFC&quot; & chr(34) & &quot;)) ORDER BY FinLog.AppSta DESC&quot;


Simon
 
I think we are on the right track. Now I come up with this error, but it has nothing to do with your SQL statement, I think.

Here is the error:Error

Type:
Microsoft OLE DB Provider for ODBC Drivers (0x80040E10)
[Microsoft][ODBC Microsoft Access Driver] Too few parameters. Expected 2.
/please.asp, line 33

Here is that script:


<%
Param = Request.QueryString(&quot;Param&quot;)
Data = Request.QueryString(&quot;Data&quot;)
%>
<%

Set conn = Server.CreateObject(&quot;ADODB.Connection&quot;)
conn.open &quot;ONLAPPCYC2K&quot;,&quot;&quot;,&quot;&quot;
Set Session(&quot;ONLAPPCYC2K_conn&quot;) = conn

%>
<%
sql = Request (&quot;sql&quot;)
if sql = &quot;&quot; then
sql = &quot;SELECT FinLog.AppSta, Count(FinLog.SitID) AS Records FROM FinLog GROUP BY FinLog.AppSta, FinLog.SitID HAVING (((FinLog.SiteID)=&quot; & chr(34) & &quot;CY2KFC&quot; & chr(34) & &quot;)) ORDER BY FinLog.AppSta DESC&quot;




If cstr(Param) <> &quot;&quot; And cstr(Data) <> &quot;&quot; Then
sql = sql & &quot; And [&quot; & cstr(Param) & &quot;] = &quot; & cstr(Data)
End If
End If
Set rs = Server.CreateObject(&quot;ADODB.Recordset&quot;)
rs.Open sql, conn, 3, 3
%>

I appreciate all this help..

~Chris
 
This error means that you are truing to reference a field in the SQl statement that does not exist in the table.

I notice that you have Count(FinLog.SitID) instead of Count(FinLog.SiteID).

And the same error in the GROUP BY clause.

Change this and see what happens.

Simon
 
This seems to work:

sql = &quot;SELECT FinLog.AppSta, Count(FinLog.SitID) AS Records FROM FinLog GROUP BY FinLog.AppSta, FinLog.SitID HAVING (((FinLog.SitID)='CY2KFC')) ORDER BY FinLog.AppSta DESC&quot;

The good thing is that I no longer have errors, the bad part is that I must not be requesting the data right because it's just blank.

Here is what I am using:

<%=Server.HTMLEncode(rs.Fields(&quot;AppSta&quot;).Value)%>

is this right?

~Chris
 
What do you want to do with the returned records??

Server.HTMLEncode(...) is used for encoding strings that you are going to use in URL's, so that spaces get converted to %20, ....

If you want to output the results in a table, put the following code immediately after the line rs.Open ... and immediately before the last %> in the code posted in your message beginning &quot;I think we are on the right track...&quot;:

%>
<table>
<tr>
<td colspan=&quot;2&quot;>Report for site ID &quot;CY2KFC&quot;</td>
</tr>
<%
Do Until rs.EOF
Response.Write &quot;<tr><td>Value &quot; & rs(&quot;AppSta&quot;) & &quot;</td><td>&quot; & rs(&quot;Records&quot;) & &quot; Records</td></tr>&quot; & chr(13)
rs.MoveNext
Loop
%>
</table>
<%
rs.Close
conn.Close
Set conn = Nothing


I also do not know what the following code is meant to do:

If cstr(Param) <> &quot;&quot; And cstr(Data) <> &quot;&quot; Then
sql = sql & &quot; And [&quot; & cstr(Param) & &quot;] = &quot; & cstr(Data)
End If


Simon
 
Yes, I want to output the results in a table. However I am still geting nothing. It just reads (Report for site ID &quot;CY2KFC&quot;), but nothing else.

Here is the full code:
<html>
<head>
<meta HTTP-EQUIV=&quot;Content-Type&quot; CONTENT=&quot;text/html;charset=windows-1252&quot;>
<title>test</title>
</head>
<body>
<body BGCOLOR=&quot;FFFFFF&quot;>

<%
Param = Request.QueryString(&quot;Param&quot;)
Data = Request.QueryString(&quot;Data&quot;)
%>
<%

Set conn = Server.CreateObject(&quot;ADODB.Connection&quot;)
conn.open &quot;ONLAPPCYC2K&quot;,&quot;&quot;,&quot;&quot;
Set Session(&quot;ONLAPPCYC2K_conn&quot;) = conn

%>
<%
sql = Request (&quot;sql&quot;)
if sql = &quot;&quot; then
sql = &quot;SELECT FinLog.AppSta, Count(FinLog.SitID) AS Records FROM FinLog GROUP BY FinLog.AppSta, FinLog.SitID HAVING (((FinLog.SitID)='CY2KFC')) ORDER BY FinLog.AppSta DESC&quot;


End If
Set rs = Server.CreateObject(&quot;ADODB.Recordset&quot;)
rs.Open sql, conn, 3, 3
%>
<table>
<tr>
<td colspan=&quot;2&quot;>Report for site ID &quot;CY2KFC&quot;</td>
</tr>
<%
Do Until rs.EOF
Response.Write &quot;<tr><td>Value &quot; & rs(&quot;AppSta&quot;) & &quot;</td><td>&quot; & rs(&quot;Records&quot;) & &quot; Records</td></tr>&quot; & chr(13)
rs.MoveNext
Loop
%>
</table>
<%
rs.Close
conn.Close
Set conn = Nothing

%>


Thanks again for all of this help,
~ Chris

 
Send me the database - e mail address swilliams@paragon.bm - so that I can check the SQL statement (remember to compact the database, and if it is still big, zip it as well)

Simon
 
The problem now is the way that you have the tables linked behind the scene. FinLog.SitID does NOT hold the site reference CY2KFC. This references the table &quot;SiteID&quot;. If you look in there, the ID for CY2KFC is 1, and so the sql statement:

sql = &quot;SELECT FinLog.AppSta, Count(FinLog.SitID) AS Records FROM FinLog GROUP BY FinLog.AppSta, FinLog.SitID HAVING (((FinLog.SitID)='1')) ORDER BY FinLog.AppSta DESC&quot;

works as we were expecting before.

Simon
 
Simon,
Thank you so much you really saved me. Please let me know where I can send a check because you deserve to have some compensation. The last question I have is how to make it so that a dealer can enter his site ID into a form and have that submit to the asp page and bring up the appropriate values?

Thanks,
Chris
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top