SELECT c.objectivetypedescription,a.tcount,b.ccount FROM
(
select
count(*) AS tcount
from oballotlist t
join objectivetype ob on ob.objectivetype_id=t.campaign_id
LEFT JOIN oblist o ON o.OBJECTIVE_GUID = t.Objective_guid
where t.ManagerID='3D0569'
and (t.TLID = '6557AF' or '6557AF' = '')
AND t.tmrid IS NULL
and ob.objectivetype_id in ('SZ0109')
) a,
(
select
count(*) as ccount
from oballotlist t
left join objectivetype ob on ob.objectivetype_id=t.campaign_id
LEFT JOIN oblist o ON o.OBJECTIVE_GUID = t.Objective_guid
where t.ManagerID='3D0569'
and (t.TLID = '6557AF' or '6557AF' = '')
AND t.tmrid IS NOT NULL
AND o.TLallot = '6557AF'
AND o.ATTEMPT = 0 AND o.event_guid IS NULL
and ob.objectivetype_id in ('SZ0109')
) b,
(SELECT ob.objectivetypedescription FROM objectivetype ob WHERE ObType = 1 and ob.objectivetype_id in ('SZ0109')
) c
结果如下:
objectivetypedescription tcount ccount
淘宝旗舰一套餐140429 0 2
如果and ob.objectivetype_id in ('SZ0109') 这个条件是一个,则可行,但多个无法一起显示对应的统计,请问有办法么?
例子:and ob.objectivetype_id in ('SZ0109','SZ0110')的话,想显示成如下:
objectivetypedescription tcount ccount
淘宝旗舰一套餐140429 0 2
淘宝旗舰一套餐140430 12 5
------解决思路----------------------
每个子查询都包含 objectivetype_id,然后关联起来
SELECT c.objectivetypedescription,a.tcount,b.ccount
FROM
(
select ob.objectivetype_id,
...
and ob.objectivetype_id in ('SZ0109','SZ0110')
) a,
(
select ob.objectivetype_id,
...
and ob.objectivetype_id in ('SZ0109','SZ0110')
) b,
(
select ob.objectivetype_id,
...
and ob.objectivetype_id in ('SZ0109','SZ0110')
) c
WHERE c.objectivetype_id = a.objectivetype_id
AND c.objectivetype_id = b.objectivetype_id
------解决思路----------------------
这样就少了
select
case when ob.objectivetype_id in ('SZ0109') then ob.objectivetypedescription else '' end ,
case when t.ManagerID='3D0569'
and (t.TLID = '6557AF' or '6557AF' = '')
AND t.tmrid IS NULL
and ob.objectivetype_id in ('SZ0109') then count(*) else 0 end as tcount,
case when t.ManagerID='3D0569'
and (t.TLID = '6557AF' or '6557AF' = '')
AND t.tmrid IS NOT NULL
AND o.TLallot = '6557AF'
AND o.ATTEMPT = 0 AND o.event_guid IS NULL
and ob.objectivetype_id in ('SZ0109') then count(*) else 0 end as ccount
from oballotlist t
join objectivetype ob
on ob.objectivetype_id=t.campaign_id
JOIN oblist o ON o.OBJECTIVE_GUID = t.Objective_guid
where ObType = 1
------解决思路----------------------
#3 的3各子查询各自的结果是什么?
如果记录数不一致,那么要用
SELECT ...
FROM (...) c
LEFT JOIN (...) a
ON c.objectivetype_id = a.objectivetype_id
LEFT JOIN (...) b
ON c.objectivetype_id = b.objectivetype_id