Question : Count and display the number of times the same entry appears

I have a DB with a table with 3 fields: BookID, BookTitle and BookVendor. There can be duplicate BookVendors entered. I want to display (only once each BookVendor) and then display the number of times it appears in the table. I don't think my sql WHERE statement is correct and I am not sure how to code the display for the results.

Example:
Vendor1:  10
Vendor2: 8
Vendor3: 1

Following is my code:
___________________


 
<%
 
'Number of records for each vendor
sqlBookVendorCount = "SELECT count(*) AS BookVendorCount " & _
            "FROM tblBooks " & _
            "WHERE BookVendor = BookVendor "
           
Set objBookVendorCount = objConn.Execute(sqlBookVendorCount)
 
%>
 
 
 

 




 

 

<%=title%>


 

Following is the breakdown of VENDORS for each of the online resources
 



 


     
  • Book Vendors
      <%=objBookVendor("BookVendor")%>:  <%=objBookVendorCount("BookVendorCount")%>>
     

 
 

 

 


 













;

Answer : Count and display the number of times the same entry appears

Since there may be more than one row of data in your resultset, you need to loop through your results. See the attached code.

Generally, I would first make sure that the SQL query works correctly using a SQL Server client tool (such as Query Analyzer in SQL Server Management Studio) before testing a page with programming code employing that SQL query.

1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
<%
	' List number of books for each vendor.
	' =====================================
	
	Dim strSQL_getBooksCountForEachVendor, objRS_getBooksCountForEachVendor
		
	strSQL_getBooksCountForEachVendor = "SELECT BookVendor, COUNT(*) AS BookVendorCount FROM tblBooks GROUP BY BookVendor"
				
	Set objRS_getBooksCountForEachVendor = objConn.Execute(strSQL_getBooksCountForEachVendor)
				
	If (objRS_getBooksCountForEachVendor.EOF) Then
		' No rows available to display.	
	Else
		' Loop through all of the rows and display each row.
		' (Using HTML Table as container for displaying SQL data results.)
%>
	
<%				
	Dim strBookVendor, iBookVendorCount
	
	Do While (NOT objRS_getBooksCountForEachVendor.EOF)								
		strBookVendor = Trim( objRS_getAllNewsItems("BookVendor") )
		iBookVendorCount = Trim( objRS_getAllNewsItems("BookVendorCount") )
					
		'Now, display the current row.
%> 
  

<%					
		'Move on to the next row.
		objRS_getBooksCountForEachVendor.MoveNext()
	Loop 
%>
	
<%=strBookVendor%> <%=iBookVendorCount%>
<% End If 'Clean up objRS_getBooksCountForEachVendor.Close Set objRS_getBooksCountForEachVendor = Nothing %>
Random Solutions  
 
programming4us programming4us