[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

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

Posted on 2009-06-27
5
Medium Priority
?
282 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 1000 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 1000 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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

Introduction This article will provide a solution for an error that might occur installing a new SQL 2005 64-bit cluster. This article will assume that you are fully prepared to complete the installation and describes the error as it occurred durin…
Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
This course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…

656 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