Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Please Help

Author  Topic 

norty911
Starting Member

41 Posts

Posted - 2007-05-07 : 05:09:36
Hey guys, is it possible to subtract the results from two queries and return the result of that?

Say for instance you have a stored proc that counts the rows of two tables... how could you add another statement to that stored proc so that it subtracts the row count from the two tables?

Thanks!

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2007-05-07 : 05:13:35
RETURN @@ROWCOUNT - 2 ?


Peter Larsson
Helsingborg, Sweden
Go to Top of Page

norty911
Starting Member

41 Posts

Posted - 2007-05-07 : 05:17:05
Sorry to clarify something like (row count from tbl1) - (row count from tbl2) = result
Go to Top of Page

harsh_athalye
Master Smack Fu Yak Hacker

5581 Posts

Posted - 2007-05-07 : 05:17:43
[code]select
sum(case when t = 1 then cnt else 0 end - case when t = 2 then cnt else 0 end) as diff
From
(
select 1 as t, count(*) as cnt from table1
union all
select 2 as t, count(*) as cnt from table2
) t[/code]

Harsh Athalye
India.
"The IMPOSSIBLE is often UNTRIED"
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2007-05-07 : 05:21:09
This???

select max(cnt) - min(cnt)
from
(
select 1 as t, count(*) as cnt from table1
union all
select 2, count(*) from table2
) as t


Peter Larsson
Helsingborg, Sweden
Go to Top of Page

pbguy
Constraint Violating Yak Guru

319 Posts

Posted - 2007-05-07 : 05:35:37
select ABS((select count(*) from <table 1>) - (select count(*) from <table2>))
Go to Top of Page

norty911
Starting Member

41 Posts

Posted - 2007-05-07 : 05:43:00
Thank you guys for the reply... pbguy put me in the right direction.

Another quick q... taking the same example... you have two select statements in a stored proc. How do you return multiple values from the stored procedure? So instead of subtracted the two row counts in the stored proc... I can do it in my actual c# code by retrieving the two return values?
Go to Top of Page

harsh_athalye
Master Smack Fu Yak Hacker

5581 Posts

Posted - 2007-05-07 : 05:46:00
You can declare two OUTPUT parameters and return values of COUNT(*) using them to the front-end.

Harsh Athalye
India.
"The IMPOSSIBLE is often UNTRIED"
Go to Top of Page

pbguy
Constraint Violating Yak Guru

319 Posts

Posted - 2007-05-07 : 05:47:31
concatenate the two value with the special charaters like 5$$$$10 and find the position of '$$$$' and extract the values in ur UI
Go to Top of Page

harsh_athalye
Master Smack Fu Yak Hacker

5581 Posts

Posted - 2007-05-07 : 06:12:37
But beware of this approach of passing values from back-end to front-end, especially if the count is for the tables which undergoes frequent DML operations, as by the time you pass the values from back-end to front-end, the count might become stale and thus wrong.

Harsh Athalye
India.
"The IMPOSSIBLE is often UNTRIED"
Go to Top of Page

norty911
Starting Member

41 Posts

Posted - 2007-05-07 : 06:20:18
Thank you all, much appreciated
Go to Top of Page
   

- Advertisement -