日期:2014-05-18  浏览次数:20448 次

继续发帖求一个SQL语句的编写或优化
订单信息表t1:order_id(订单号) add_time(下单时间) tourist_id(客户信息)
客户信息表t2: tourist_id(客户信息) tel1 tel2 tel3 tel4(四个电话或手机号码)
呼出信息表t3: tel(呼出电话) calltime(呼出时间)

其中t1中tourist_id关联到t2中tourist_id,t3中的tel可能为t2中的任一个

查询过程:根据order_id找到4个号码,根据4个号码找到对应到该order_id的呼出时间,然后用calltime-add_time得到呼出时间差

查询结果: 订单号 下单时间 第一次呼出时间差 第二次呼出时间差 第三次呼出时间差

望高手解答!

或者将下面的SQL语句优化下,昨天SQLCenter回复的一个SQL语句我今天执行了一下,发现效率非常低,几分钟都没有结果,被我取消了,而且我才导了不到2个月的数据,大概70W条记录,希望能优化下,急盼回复!

SQL code
with cte as
(
    select id=row_number()over(partition by t1.order_id order by t3.calltime),
        t1.order_id, t1.add_time, diff=datediff(second, t1.add_time, t3.calltime)
    from t1
        join t2 on t1.tourist_id=t2.tourist_id
        join t3 on t2.tel1=t3.tel or t2.tel2=t3.tel or t2.tel3=t3.tel or t2.tel4=t3.tel
)
select order_id,
    [1] = max(case id when 1 then diff end),
    [2] = max(case id when 2 then diff end),
    [3] = max(case id when 3 then diff end)
from cte group by order_id




------解决方案--------------------
注意加索引试试呢?
------解决方案--------------------
更正下
[code=SQL][/code]
select a.order_id
,a.add_time
,[call_1]=max(case when c.id=1 then datediff(second, a.add_time,c.calltime) end)
,[call_2]=max(case when c.id=2 then datediff(second, a.add_time,c.calltime) end)
,[call_3]=max(case when c.id=3 then datediff(second, a.add_time,c.calltime) end)
from t1 a inner join
t2 b on a.tourist_id=b.tourist_id cross apply
(select top 3 id=row_number() over(order by calltime),calltime from t3 where tel=b.tel1 or tel=b.tel2 or tel=b.tel3 or tel=b.tel4 order by calltime) c
group by a.order_id,a.add_time
[code=SQL][/code]
------解决方案--------------------
除非你一个客人只能下一次订单,否则表设计我想有点问题!

因为t3没有明确说是给哪张订单打电话,如果用你写的那种逻辑,同一个客人所有的订单都是按最后三次打电话的时间来计算,结果永远不对

------解决方案--------------------
还是我来解释一下吧
id=row_number()over(partition by t1.order_id order by t3.calltime)
每个 order_id 对应 的 id 是唯一的,只有一条记录,min/max/sum都是它
[1] = min(case id when 1 then diff end)
[1] = max(case id when 1 then diff end)
[1] = sum(case id when 1 then diff end)
一样的效果

为什么要使用聚合函数呢
SQL code
1     2     3
diff1 null  null
null  diff2 null
null  null  diff3

------解决方案--------------------
客户信息表t2: tourist_id(客户信息) tel1 tel2 tel3 tel4(四个电话或手机号码)
-----------
速度瓶颈应该在这里,如果有一个这样的表:
t2_tel:
SQL code
id  tourist_id tel
1   1          tel1
2   1          tel2
3   1          tel3
4   1          tel4
5   2          tel1
6   2          tel2
7   2          tel3
8   3          tel1
。。。

------解决方案--------------------
with r as (
select a.order_id
,a.add_time
,c.calltime
from t1 a inner join
t2 b on a.tourist_id=b.tourist_id cross apply
(select top 3 calltime from t3 where tel=b.tel1 and calltime>a.add_time/*锁定add_time后的电话时间*/ order by calltime) c
union all
select a.order_id
,a.add_time
,c.calltime
from t1 a inner join
t2 b on a.tourist_id=b.tourist_id cross apply
(select top 3 calltime from t3 where tel=b.tel2 and calltime>a.add_time/*锁定add_time后的电话时间*/ order by calltime) c
union all
select a.order_id
,a.add_time
,c.calltime
from t1 a inner join
t2 b on a.tourist_id=b.tourist_id cross apply
(select top 3 calltime from t3 where tel=b.tel3 and calltime>a.add_time/*锁定add_time后的电话时间*/ order by calltime) c