Я создаю 2 запроса:
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
Я получаю следующие результаты для вышеупомянутых 2 запросов
результат для q1:
cid1 pname1 uname1
11 07/2011 admin
5 08/2011 admin
9 09/2011 admin
результат для q2:
cid2 pname2 uname2
9 07/2011 admin
3 09/2011 admin
после объединения обоих запросов я получаю следующий вывод:
cid1 cid2 pname1 pname2 uname1 uname2
11 9 07/2011 07/2011 admin admin
9 3 09/2011 09/2011 admin admin
Но яхотите получить результат как
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
Как это сделать?
Мой комбинированный запрос приведен ниже:
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