Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Cross reference categories will performing a count

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

Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
data stored like ????? in sql server column 19 54
Re-appearing SQL Server Agent jobs 7 29
SQL Backup Question 2 29
Access SQL Server instance by NONE-admin Windows user 12 28
Nowadays, some of developer are too much worried about data. Who is using data, who is updating it etc. etc. Because, data is more costlier in term of money and information. So security of data is focusing concern in days. Lets' understand the Au…
In this article we will learn how to fix  “Cannot install SQL Server 2014 Service Pack 2: Unable to install windows installer msi file” error ?
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
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.

856 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