Solved

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

Posted on 2009-06-27
5
245 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
ID: 24727706
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
ID: 24727725
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
ID: 24730501
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
ID: 24774196
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
ID: 24776053
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

U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

Question has a verified solution.

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

If you having speed problem in loading SQL Server Management Studio, try to uncheck these options in your internet browser (IE -> Internet Options / Advanced / Security):    . Check for publisher's certificate revocation    . Check for server ce…
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.
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …

911 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

19 Experts available now in Live!

Get 1:1 Help Now