这句SQL语句还能优化吗?

liumang9527 2007-09-13 09:36:02

select * from virtel_telrecord t
where t.sn in
(select b.sn from virtel_telrecord b ,virtel_telrecord c
where b.source = c.source and b.dest = c.dest
and b.sn !=c.sn and b.status = 'Z'
and c.end_time = b.start_time)

帮帮忙
...全文
122 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
liumang9527 2007-09-14
  • 打赏
  • 举报
回复
select ENTERPRISE_NUM ,
SUM(CASE WHEN STATUS = 'A' then 1 else 0 end) ,
SUM(CASE WHEN STATUS = 'Z' then 1 else 0 end)
from VIRTEL_TELRECORD
where START_TIME >= '2006-8-2 10:35:12'
and sn not in(select b.sn from virtel_telrecord b ,virtel_telrecord c
where b.source = c.source and b.dest = c.dest
and b.sn !=c.sn and b.status = 'Z'
and c.end_time = b.start_time)
group by ENTERPRISE_NUM

其实是要优化这个SQL
Limpire 2007-09-14
  • 打赏
  • 举报
回复
-- try

select a.ENTERPRISE_NUM ,
SUM(CASE WHEN a.status = 'A' then 1 else 0 end) ,
SUM(CASE WHEN a.status = 'Z' then 1 else 0 end)
from
virtel_telrecord a
left join
(select b.sn from virtel_telrecord b ,virtel_telrecord c
where b.source = c.source and b.dest = c.dest
and b.sn !=c.sn and b.status = 'Z'
and c.end_time = b.start_time) b
on a.sn = b.sn
where a.start_time >= '2006-8-2 10:35:12' and b.sn is null
group by a.ENTERPRISE_NUM
liumang9527 2007-09-13
  • 打赏
  • 举报
回复
to: dawugui(潇洒老乌龟)

--不过最后一个b.sn != c.sn,这个条件是否有点奇怪.
---------------------------------------------------
去除 end_time = start_time 的记录
cxmcxm 2007-09-13
  • 打赏
  • 举报
回复
直接连接就行

select b.* from virtel_telrecord b ,virtel_telrecord c
where b.source = c.source and b.dest = c.dest
and b.sn !=c.sn and b.status = 'Z'
and c.end_time = b.start_time
Lyw110 2007-09-13
  • 打赏
  • 举报
回复
select * --尽量别写*,写全列可以用到索引,而*用不到索引
from virtel_telrecord t
where exists(select * --exists 快过 in
from virtel_telrecord b ,virtel_telrecord c
where b.source = c.source and b.dest = c.dest
and b.sn !=c.sn and b.status = 'Z'
and t.sn = b.sn)
dawugui 2007-09-13
  • 打赏
  • 举报
回复
select b.* from virtel_telrecord b , virtel_telrecord c
where b.source = c.source and
b.dest = c.dest and
b.start_time = c.end_time and
b.status = 'Z' and b.sn != c.sn


--不过最后一个b.sn != c.sn,这个条件是否有点奇怪.
Limpire 2007-09-13
  • 打赏
  • 举报
回复
select b.* from virtel_telrecord b left join virtel_telrecord c
on b.source = c.source and b.dest = c.dest
where b.sn !=c.sn and b.status = 'Z'
and c.end_time = b.start_time

34,575

社区成员

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

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