?
Solved

Cross reference categories will performing a count

Posted on 2003-11-20
5
Medium Priority
?
228 Views
Last Modified: 2010-07-27
Hello all,

I have two tables.  One is a list of Categories, the other is a list of items with those categories assigned to each item.  There is a one to many relationship between the two tables.

tblCategories
---------------------
catName
catID

tblItems
---------------
ItemID
ItemName
catID

I need to count the number of items in tblItems for each category that is contained in tblCategories.  Moreover, I need to retrieve this data so that I can display it on a webpage, so I need to return, the catID and the Count of items with that catID.  I want to use a stored procedure.

---Desired Output------
catID     Count
1              22
2              109
3              67
etc. . .
--------------------------


Can someone please help me?
0
Comment
Question by:knottydrd
  • 3
  • 2
5 Comments
 
LVL 5

Expert Comment

by:morpheus30
ID: 9792136
CREATE PROCEDURE sp_CatItemCount AS

SELECT CatName, COUNT(ItemID) AS CountOfItem
FROM tblCategories C
INNER JOIN tblItems I ON C.CatID = I.CatID
GROUP BY CatName
0
 
LVL 5

Expert Comment

by:morpheus30
ID: 9792207
oopss...  You wanted CatID not CatName

CREATE PROCEDURE sp_CatItemCount AS

SELECT C.CatID, COUNT(ItemID) AS CountOfItem
FROM tblCategories C
INNER JOIN tblItems I ON C.CatID = I.CatID
GROUP BY CatName

Then on your web page you could do this...

<%

Dim oCn
Dim rs
Dim sql

sql = "sp_CatItemCount"

Set oCn = Server.CreateObject("ADODB.Connection")
oCn.Open "DSN=DSNName; uid=username; pwd=password"

Set rs = Server.CreateObject("ADODB.Recordset")
rs.Open sql, oCn, 1, 1, adCmdStoredProc

%>

<table>
<tr>
<td><%= rs(0).Name %></td><td><%= rs(1).Name %></td>
</tr>

<%
Do until rs.eof
%>

<tr>

<%
For x = 0 to rs.Fields.Count - 1
%>

<td><%= rs(x) %></td>

<%
Next
%>

</tr>

<%
rs.MoveNext
Loop
%>

</table>

rs.Close
Set rs = Nothing





0
 
LVL 5

Accepted Solution

by:
morpheus30 earned 2000 total points
ID: 9792236
oops, my bad on the above SQL Statement...
Forgot to group by C.CatID not CatName

SELECT C.CatID, COUNT(ItemID) AS CountOfItem
FROM tblCategories C
INNER JOIN tblItems I ON C.CatID = I.CatID
GROUP BY C.CatID
0
 

Author Comment

by:knottydrd
ID: 9792778
Dude, you are my freakin' hero.

That worked great, thanks.
0
 

Author Comment

by:knottydrd
ID: 9792832
Got time for one more question?
0

Featured Post

Visualize your virtual and backup environments

Create well-organized and polished visualizations of your virtual and backup environments when planning VMware vSphere, Microsoft Hyper-V or Veeam deployments. It helps you to gain better visibility and valuable business insights.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

One of the most important things in an application is the query performance. This article intends to give you good tips to improve the performance of your queries.
This shares a stored procedure to retrieve permissions for a given user on the current database or across all databases on a server.
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.
Suggested Courses

840 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