I am creating 2 queries:
1
select count(c.id) as cid1, p.name as pname1, u.name as uname1
from crm_lead c, account_period p, res_users u
where c.create_date between p.date_start and p.date_stop and
(c.user_id = u.id or c.sales_vertical=u.id) and
u.id = 1
group by p.name, u.name
2
select count(c.id) as cid2, p.name as pname2, u.name as uname2
from crm_lead c, account_period p, res_users u
where c.create_date between p.date_start and p.date_stop and
(c.user_id = u.id or c.sales_vertical=u.id) and
stage_id =12 and
u.id = 1
where a.pname = b.pname
group by p.name, u.name
I am getting the following resuls for the above 2 queries
result for q1 :
cid1 pname1 uname1
11 07/2011 admin
5 08/2011 admin
9 09/2011 admin
result for q2 :
cid2 pname2 uname2
9 07/2011 admin
3 09/2011 admin
after combining both the queries I am getting following output:
cid1 cid2 pname1 pname2 uname1 uname2
11 9 07/2011 07/2011 admin admin
9 3 09/2011 09/2011 admin admin
But I want the result as
cid1 cid2 pname1 pname2 uname1 uname2
11 9 07/2011 07/2011 admin admin
5 0 08/2011 08/2011 admin admin
9 3 09/2011 09/2011 admin admin
How can it be done?
My combined query is below:
select a.cid1, b.cid2, a.pname1, b.pname2, a.uname1, b.uname2
from (select count(c.id) as cid1, p.name as pname1, u.name1 as uname
from crm_lead c, account_period p, res_users u
where c.create_date between p.date_start and p.date_stop and
(c.user_id = u.id or c.sales_vertical=u.id) and
u.id = 1
group by p.name, u.name) as a,
(select count(c.id) as cid2, p.name as pname2, u.name as uname2
from crm_lead c, account_period p, res_users u
where c.create_date between p.date_start and p.date_stop and
(c.user_id = u.id or c.sales_vertical=u.id) and
stage_id =12 and
u.id = 1
group by p.name, u.name)as b
where a.pname1 = b.pname2
To get your requested result the query could look like this:
I have my doubts, however, that you actually want that exact result.