Firebird 1.5联盟

时间:2017-05-23 10:16:14

标签: sql firebird firebird1.5

我正在尝试获取报告,以在每行显示不同的结果

select
count(case when call_type='I' and cl.client_ID not like 391 and c.call_start  >= '2017/04/01' and c.call_start  <= '2017/04/30'then 1 else null end) as Incoming_Main_April, 
count(case when call_type='O' and cl.client_ID not like 391 and c.call_start  >= '2017/04/01' and c.call_start  <= '2017/04/30'then 1 else null end) as Outgoing_Main_April,
count(case when call_type='I' and cl.client_ID=391 and c.call_start  >= '2017/04/01' and c.call_start  <= '2017/04/30'then 1 else null end) as Incoming_SMG_April,
count(case when call_type='O' and cl.client_ID=391 and c.call_start  >= '2017/04/01' and c.call_start  <= '2017/04/30'then 1 else null end) as Outgoing_SMG_April

from
  CALLS c 
  left outer join CONTACTS ct on c.CONTACT_ID= ct.CONTACT_ID
  left outer join clients cl on cl.client_id= ct.COMPANY_ID where cl.RECORD_STATUS='A' 
  and c.OPERATOR_ID in (1510,2938,12443,4482,8911,6947,2056,1969,1952,2223,1511,2224,2039,2055,2085,1949,5963,1502,11112,1633,2034,2057)  

order by
  count(call_type)
例如,上面的内容将给出4列,其中包含1行结果。 现在我想在第二行有三月(例如)......依此类推。

建议?

1 个答案:

答案 0 :(得分:0)

Imho,你最好使用GROUP BY

select
  extract(month from c.call_start) as MyMonth,
  count(case when call_type='I' and cl.client_ID not like 391 then 1 else null end) as Incoming_Main, 
  count(case when call_type='O' and cl.client_ID not like 391 then 1 else null end) as Outgoing_Main,
  count(case when call_type='I' and cl.client_ID = 391        then 1 else null end) as Incoming_SMG,
  count(case when call_type='O' and cl.client_ID = 391        then 1 else null end) as Outgoing_SMG
from
  CALLS c 
  left outer join CONTACTS ct on c.CONTACT_ID= ct.CONTACT_ID
  left outer join clients cl on cl.client_id= ct.COMPANY_ID
where cl.RECORD_STATUS='A' 
  and c.OPERATOR_ID in (1510,2938,12443,4482,8911,6947,2056,1969,1952,2223,1511,2224,2039,2055,2085,1949,5963,1502,11112,1633,2034,2057)
  and extract(month from c.call_start) between 3 and 4 -- only return entries from March and April
group by
  extract(month from c.call_start)
order by
  count(call_type)

请注意,我在selectwheregroup by之后的评估方式包括EXTRACT()