为什么得到的查询结果不正确,请指教
table1(订单表) table2
id agent_id ..... id agent_id type
1 101 1 101 咨询数
2 102 2 102 报号数
3 103 3 101 报号数
4 102
5 101
想实现如下结果
工号 订单数 报号数 咨询数 总数 订单率
101 2 1 1 4 50.00%
102 2 1 0 3 66.7%
103 1 0 0 1 100%
我写的SQL语句为:
select a.*,b.报号数,b.咨询数 from(select 工号=agent_id,订单数=count(*)
from table1 group by agent_id) a join (select a.agent_id,报号数=sum(case
when type='报号'then 1 else 0 end),咨询数=sum(case when type='咨询'then 1
else 0 end) from table1 a join table2 b on a.agent_id=b.agent_id group
by a.agent_id) b on a.工号=b.agent_id
但总是返回错误的结果.
请大家指点,谢谢