Solved

Return rows with a column that tells how many times id is in another table

Posted on 2009-06-27
5
223 Views
Last Modified: 2012-05-07
I have 2 tables. I want to return everything from Table1 where ItemId=2. I want to add one column CountUnits that tells how many times UnitId exists in Table2. How do I write a stored procedure for this?

Table 1
T1Id, ItemId, UnitId, T1Name
Table 2
T2Id, UnitId, T2Name.

Parameter: ItemId
Return:   T1Id, ItemId, UnitId,T1Name, CountUnits
0
Comment
Question by:johnkainn
  • 2
  • 2
5 Comments
 
LVL 26

Expert Comment

by:tigin44
Comment Utility
this query will give you te result

select t1.T1Id, t1.ItemId, t1.UnitId, t1.T1Name, count(*) as CountUnits
from table1 t1
         inner join table2 t2 on t1.UnitId = t2.UnitId
where t1.ItemId = 2
group by t1.T1Id, t1.ItemId, t1.UnitId, t1.T1Name
0
 
LVL 26

Accepted Solution

by:
tigin44 earned 250 total points
Comment Utility
and as a spit can be like this

CREATE PROCEDURE spCountUnits
    @itemId               int,
    @countUnits        int OUTPUT
AS

select t1.T1Id, t1.ItemId, t1.UnitId, t1.T1Name, @countUnits = count(*)
from table1 t1
         inner join table2 t2 on t1.UnitId = t2.UnitId
where t1.ItemId = @itemId
group by t1.T1Id, t1.ItemId, t1.UnitId, t1.T1Name;



and you can call it like

declare @countUnit    int;
exec  spCountUnits  2(will be your itemId), @countUnit OUTPUT;
select @countUnit;


0
 
LVL 5

Expert Comment

by:rizwanidrees
Comment Utility
CREATE PROCEDURE spCountUnits
    @itemId               int
AS

select t1.T1Id, t1.ItemId, t1.UnitId, t1.T1Name, count(t2.UnitId) countUnits
from table1 t1
         inner join table2 t2 on t1.UnitId = t2.UnitId
where t1.ItemId = @itemId
group by t1.T1Id, t1.ItemId, t1.UnitId, t1.T1Name;
0
 

Author Comment

by:johnkainn
Comment Utility
Thanks. If I use this I only get returned the rows with matching unitid in table 2.
I would like to get all rows returned from table 1. If there is no mathching unitId in table 2 then I would like to have "0" in CountUnits column, but otherwise the number of times particular unitId appears in table 2.
For example if unitId=2 is in 3 rows in table 2 CountUnits=3. If there is no row with unitid=3 in Table 2 then CountUnits=0.


0
 
LVL 5

Assisted Solution

by:rizwanidrees
rizwanidrees earned 250 total points
Comment Utility
CREATE PROCEDURE spCountUnits
    @itemId               int
AS

select t1.T1Id, t1.ItemId, t1.UnitId, t1.T1Name, count(t2.UnitId) countUnits
from table1 t1
         left join table2 t2 on t1.UnitId = t2.UnitId
where t1.ItemId = @itemId
group by t1.T1Id, t1.ItemId, t1.UnitId, t1.T1Name;
0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

by Mark Wills Attending one of Rob Farley's seminars the other day, I heard the phrase "The Accidental DBA" and fell in love with it. It got me thinking about the plight of the newcomer to SQL Server...  So if you are the accidental DBA, or, simp…
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

743 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

16 Experts available now in Live!

Get 1:1 Help Now