| 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 LarssonHelsingborg, Sweden |
 |
|
|
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 |
 |
|
|
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 diffFrom(select 1 as t, count(*) as cnt from table1union allselect 2 as t, count(*) as cnt from table2) t[/code]Harsh AthalyeIndia."The IMPOSSIBLE is often UNTRIED" |
 |
|
|
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 table1union allselect 2, count(*) from table2) as tPeter LarssonHelsingborg, Sweden |
 |
|
|
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>)) |
 |
|
|
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? |
 |
|
|
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 AthalyeIndia."The IMPOSSIBLE is often UNTRIED" |
 |
|
|
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 |
 |
|
|
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 AthalyeIndia."The IMPOSSIBLE is often UNTRIED" |
 |
|
|
norty911
Starting Member
41 Posts |
Posted - 2007-05-07 : 06:20:18
|
| Thank you all, much appreciated |
 |
|
|
|