Solved

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

Posted on 2009-06-27
5
273 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
[X]
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
  • 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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Recently, when I was asked to create a new SQL 2005 cluster, Microsoft released a new service pack for MS SQL 2005 what is Service Pack 3. When I finished the installation of MS SQL 2005 I found myself troubled why the installation of SP3 failed …
I am showing a way to read/import the excel data in table using SQL server 2005... Suppose there is an Excel file "Book1" at location "C:\temp" with column "First Name" and "Last Name". Now to import this Excel data into the table, we will use…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.

695 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