为什么得到的查询结果不正确,请指教

bluesky1980 2003-12-21 05:23:11
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
但总是返回错误的结果.
请大家指点,谢谢
...全文
67 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
bluesky1980 2003-12-21
  • 打赏
  • 举报
回复
谢谢各位的帮助
shuiniu 2003-12-21
  • 打赏
  • 举报
回复
--改一下,ok!
select a.agent_id 工号 ,a.订单数,isnull(b.报号数,0) 报号数 ,isnull(b.咨询数,0) 咨询数 ,(a.订单数 + isnull(b.报号数,0) + isnull(b.咨询数,0)) 总数, (a.订单数 * 1.0/(a.订单数 + isnull(b.报号数,0) + isnull(b.咨询数,0))) 订单率 from
(
select agent_id,订单数=count(*)
from @table1 group by agent_id
) a
left join
(
select b.agent_id,
报号数=sum(case when type='报号数'then 1 else 0 end),
咨询数=sum(case when type='咨询数'then 1 else 0 end)
from (select distinct agent_id from @table1) a join @table2 b
on a.agent_id=b.agent_id group by b.agent_id
) b on a.agent_id = b.agent_id
bluesky1980 2003-12-21
  • 打赏
  • 举报
回复
shuiniu(飞扬的梦) :
若有一工号,他只有订单数,而报号和咨询数均为0时,执行您的SQL语句后,则总数和订单率这两栏就不显示数据了(都为空)
bluesky1980 2003-12-21
  • 打赏
  • 举报
回复
wzh1215(四脚蛇) :
您的SQL语句执行后不返回 103 1 0 0 1 100% 这条记录
即:在table1表中有记录而在table中无记录的agent_id的情况查询不出来
shuiniu 2003-12-21
  • 打赏
  • 举报
回复
--测试环境
declare @table1 table(id int identity, agent_id char(3))
declare @table2 table(id int identity, agent_id char(3),type char(6))
--测试数据
insert @table1 values ('101')
insert @table1 values ('102')
insert @table1 values ('103')
insert @table1 values ('102')
insert @table1 values ('101')
insert @table2 values ('101','咨询数')
insert @table2 values ('102','报号数')
insert @table2 values ('103','报号数')
--实现
select a.agent_id 工号 ,a.订单数,isnull(b.报号数,0) 报号数 ,isnull(b.咨询数,0) 咨询数 ,(a.订单数 + 报号数 + 咨询数) 总数, (a.订单数 * 1.0/(a.订单数 + 报号数 + 咨询数)) 订单率 from
(
select agent_id,订单数=count(*)
from @table1 group by agent_id
) a
left join
(
select b.agent_id,
报号数=sum(case when type='报号数'then 1 else 0 end),
咨询数=sum(case when type='咨询数'then 1 else 0 end)
from (select distinct agent_id from @table1) a join @table2 b
on a.agent_id=b.agent_id group by b.agent_id
) b on a.agent_id = b.agent_id

/*
工号 订单数 报号数 咨询数 总数 订单率
---- ----------- ----------- ----------- ----------- ----------------
101 2 0 1 3 .666666666666
102 2 1 0 3 .666666666666
103 1 1 0 2 .500000000000

(所影响的行数为 3 行)
*/
bluesky1980 2003-12-21
  • 打赏
  • 举报
回复
我的贴子写的太急了(忘记写出订单率和总数)
应该为 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) ,总数=count(a.id)+sum(case
when type='报号'then 1 else 0 end)+sum(case when type='咨询'then 1
else 0 end),订单率=cast(cast(count(a.id)/count(a.id)+sum(case
when type='报号'then 1 else 0 end)+sum(case when type='咨询'then 1
else 0 end) as decimal(20,2)) as varchar)+'%' from table1 a join table2 b
on a.agent_id=b.agent_id group by a.agent_id) b on a.工号=b.agent_id
请大家继续指正
wzh1215 2003-12-21
  • 打赏
  • 举报
回复
select a.*,b.报号数,b.咨询数 from(select 工号=agent_id,订单数=count(*)
from table1 group by agent_id) a join (select agent_id,报号数=sum(case
when type='报号'then 1 else 0 end),咨询数=sum(case when type='咨询'then 1
else 0 end) from table2 group by agent_id) b on a.工号=b.agent_id
gmlxf 2003-12-21
  • 打赏
  • 举报
回复
select
工号=a.agent_id,
订单数=count(*),
报号数=sum(case when b.type='报号' then 1 else 0 end),
咨询数=sum(case when b.type='咨询' then 1 else 0 end)
from table1 a,table2 b where a.agent_id=b.agent_id group by a.agent_id

34,575

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧