Reputation: 165
So this is my problem:
I have 2 different sql select queries, which both return me a value (1425000 and 2850000) What i want to do now is the following:
Select (query1 / query 2)
from xxxxx
where xxxx;
But query 1 and query 2 are quiet complicated queries, having count and sum functions in it.
How can I solve this?
Upvotes: 1
Views: 223
Reputation: 879291
SELECT t1.val / t2.val
FROM (SELECT val
FROM foo) t1
INNER JOIN (SELECT val
FROM bar) t2
(SELECT val FROM foo)
and (SELECT val FROM bar)
can be replaced with arbitrary SELECT
statements.
See sqlfiddle for a demo.
Upvotes: 2
Reputation: 13931
You can create Views for things like this.
create view vResult1 as
select your(
complicated(
query(
here()
)
)
);
create view vResult2 as
select another(
complicated(
query(
here()
)
)
);
Then you may run them:
select vResult1/vResult2;
If you need parameters for your complicated queries - you may use stored procedures.
Upvotes: 1
Reputation: 1393
I think you'll have to use variables.. if you don't want to write it all in a single query.
SET @query1_result = (Select ... complicated query 1)
SET @query2_result = (Select ... complicated query 2)
Select @query1_result / @query2_result
Upvotes: 1
Reputation: 117345
declare @q1 int, @q2 int
select @q1 = (query1)
select @q2 = (query2)
select @q1 / @q2
Upvotes: 3