Solved

Cross reference categories will performing a count

Posted on 2003-11-20
5
220 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 500 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

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

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

Let's review the features of new SQL Server 2012 (Denali CTP3). It listed as below: PERCENT_RANK(): PERCENT_RANK() function will returns the percentage value of rank of the values among its group. PERCENT_RANK() function value always in be…
Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.
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.

895 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now