Solved

Cross reference categories will performing a count

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Dynamics crm 2011 8 45
Using a SELECT statement inside of a CASE 5 16
BULK INSERT most recent CSV 19 20
Update in Sql 7 0
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…
Introduction SQL Server Integration Services can read XML files, that’s known by every BI developer.  (If you didn’t, don’t worry, I’m aiming this article at newcomers as well.) But how far can you go?  When does the XML Source component become …
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.

759 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

18 Experts available now in Live!

Get 1:1 Help Now