100分求sql语句-------在线等

seabore 2004-09-23 01:50:18
有2个表a,b
表a
id_no(varchar型),op_time(datetime型)
12743338 Apr 1 2004 12:33PM
12743338 Apr 10 2004 12:33PM
12743340 Apr 2 2004 12:33PM
13423099 Apr 11 2004 12:33PM

表b
id_no(varchar型),op_time(datetime型)

其中,两个表中相同id_no的数据有多条.

现在,对于a表中的每条记录,求在b表中时间差最小的
也就是a.id_no=b.id_no,并且datediff(day,a.op_time,b.op_time)最小
最好效率高些,因为记录比较多,有上百万条.

谢谢!
...全文
306 28 打赏 收藏 转发到动态 举报
写回复
用AI写文章
28 条回复
切换为时间正序
请发表友善的回复…
发表回复
seabore 2004-09-23
  • 打赏
  • 举报
回复
zjcxc(邹建),谢谢,非常感谢.这就是我要的结果
zjcxc 元老 2004-09-23
  • 打赏
  • 举报
回复
结果中的第二条记录与楼主的不一样,是因为楼主给出了一个不正确的日期
zjcxc 元老 2004-09-23
  • 打赏
  • 举报
回复
--测试

--测试数据
set language english

create table ta(id_no int,op_time datetime)
insert ta select 12743338,'Apr 5 2004 12:33PM'
union all select 12743338,'Apr 23 2004 12:33PM'
union all select 12743340,'Apr 2 2004 12:33PM'
union all select 13423099,'Apr 11 2004 12:33PM'

create table tb(id_no int,op_time datetime)
insert tb select 12743338,'Apr 2 2004 12:33PM'
union all select 12743338,'Apr 21 2004 12:33PM'
union all select 12743338,'Apr 30 2004 12:33PM'
union all select 12743340,'Apr 15 2004 12:33PM'
union all select 13423099,'Apr 22 2004 12:33PM'

set language 简体中文
go

--如果ta表id_no+op_time不唯一,则用这个(效率肯定不好)
select id_no,时间差=datediff(day,op_time
,(select top 1 op_time from tb
where id_no=a.id_no and op_time>=a.op_time
order by op_time))
from ta a

--如果ta表id_no+op_time唯一,可以用这个,效率比上面的好
select a.id_no,时间差=min(datediff(day,a.op_time,b.op_time))
from ta a,tb b
where a.id_no=b.id_no
and a.op_time<=b.op_time
group by a.id_no,a.op_time
order by a.id_no,a.op_time

go

--删除测试
drop table ta,tb

/*--测试结果
id_no 时间差
----------- -----------
12743338 16
12743338 7
12743340 13
13423099 11

(所影响的行数为 4 行)


id_no 时间差
----------- -----------
12743338 16
12743338 7
12743340 13
13423099 11

(所影响的行数为 4 行)
--*/
zjcxc 元老 2004-09-23
  • 打赏
  • 举报
回复
--如果ta表id_no+op_time唯一,可以用这个,效率比上面的好
select a.id_no,时间差=min(datediff(day,a.op_time,b.op_time))
from ta a,tb b
where a.id_no=b.id_no
and a.op_time<=b.op_time
group by a.id_no,a.op_time
order by a.id_no,a.op_time
zjcxc 元老 2004-09-23
  • 打赏
  • 举报
回复
--如果ta表id_no+op_time不唯一,则用这个(效率肯定不好)

select id_no,时间差=datediff(day,op_time
,(select top 1 op_time from tb
where id_no=a.id_no and op_time>=a.op_time
order by op_time))
from ta a
zjcxc 元老 2004-09-23
  • 打赏
  • 举报
回复
12743338 Apr 31 2004 12:33PM

这个日期是不合法的.
seabore 2004-09-23
  • 打赏
  • 举报
回复
给出数据后,反而看不出楼主要的结果是什么?
答:我的最终目的是求最小时间差.


你要得到那些字段? 相差时间最小,只算 a.id_no=b.id_no and a.op_time<=b.op_time的?
答:只要个最小时间差和a表的id_no

同一id_no在两个表中都有重复,那是否以a表为基础,把所有的B表中最接近的都找出来?
答:是的,以a表为基础,把所有的B表中最接近并且大于a表的op_time的都找出来.找出来后,再做差,就是我的结果.
如果以楼主的示例数据,最终的结果应该是什么?
最终结果如下:
a.id_no 日期差
12743338 16 (是b表的第二条)
12743338 8 (是b表的第三条)
12743340 13 (是b表的第4条)
13423099 11 (是b表的第5)

id_no是a表里所有的,日期差是b表的op_time减去a表的op_time
zjcxc 元老 2004-09-23
  • 打赏
  • 举报
回复
给出数据后,反而看不出楼主要的结果是什么?

你要得到那些字段? 相差时间最小,只算 a.id_no=b.id_no and a.op_time<=b.op_time的?

同一id_no在两个表中都有重复,那是否以a表为基础,把所有的B表中最接近的都找出来?

如果以楼主的示例数据,最终的结果应该是什么?
seabore 2004-09-23
  • 打赏
  • 举报
回复
用zjcxc(邹建)提示如下错误:
Incorrect syntax near '1'
seabore 2004-09-23
  • 打赏
  • 举报
回复
用heibai520(Crazy Java)的语句,提示top 1错误
seabore 2004-09-23
  • 打赏
  • 举报
回复
表a
id_no, op_time
12743338 Apr 5 2004 12:33PM
12743338 Apr 23 2004 12:33PM
12743340 Apr 2 2004 12:33PM
13423099 Apr 11 2004 12:33PM

表b
id_no op_time
12743338 Apr 2 2004 12:33PM
12743338 Apr 21 2004 12:33PM
12743338 Apr 31 2004 12:33PM
12743340 Apr 15 2004 12:33PM
13423099 Apr 22 2004 12:33PM


比如,对a表中的第一条记录,其id_no为12743338,所以,我要到表b中找id_no也等于12743338的,并且b.op_time大于a.op_time,且两者的差最小.则找到的记录应该为b表中的第二条,时间差为16.
同样,a表中的其他记录也能等到相应的时间差.

结果如下:
a.id_no 时间差
12743338 16 (是b表的第二条)
12743338 8 (是b表的第三条)
12743340 13 (是b表的第4条)
13423099 11 (是b表的第5)

funsuzhou 2004-09-23
  • 打赏
  • 举报
回复
学习
zjcxc 元老 2004-09-23
  • 打赏
  • 举报
回复
给数据出来测试一下.

出错的话,楼主说明用的是谁的方法.
seabore 2004-09-23
  • 打赏
  • 举报
回复
怎么提示top 1错误呀?
seabore 2004-09-23
  • 打赏
  • 举报
回复
我这提示 top 1有问题呀?
TTLOVEYOU3344 2004-09-23
  • 打赏
  • 举报
回复
select a.id_no,a.op_time ,(select b.op_time from b where a.id_no=b.id_no and datediff (day,a.op_time,b.op_time)<(select min(datediff(day,a.op_time,b1.op_time)) from b b1 where b1.id_no=a.id_no and b.op_time<>b1.op_time)) as op_timeb from a
ywh25 2004-09-23
  • 打赏
  • 举报
回复
首先在两个表上建立复合索引index_complex on (id_no,op_time)
然后再:select *
from a,b
where a.id_no=b.id_no
and a.op_time=(
select top 1 op_time from a
where id_no=a.id_no and datediff(day,op_time,b.op_time)>0
order by datediff(day,op_time,b.op_time))
seabore 2004-09-23
  • 打赏
  • 举报
回复
忘了说了,我这是sybase库
seabore 2004-09-23
  • 打赏
  • 举报
回复
a和b表都可能有重复的id_no
如果,一条语句不能完成,多条也可以,通过中间表,谢谢大家
loverpyh 2004-09-23
  • 打赏
  • 举报
回复
up一下
加载更多回复(8)

34,593

社区成员

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

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