高手看过来!! 高分求查询语句!!!!! 分不够可再加!!! 走过路过不要错过!!!!

chx007 2002-08-23 04:04:56
设表tab1结构如下:
字段 类型 主键
id char(7) Y
name char(10) N

设表tab2结构如下:
字段 类型 主键 外键
id char(7) Y Y
time datetime Y N
value numeric(8,3)N N

现在要查询出每个id点,在所给定的起始时间到终止时间的区间中,

最大时间(<=终止时间)所在的value减去最小时间(>=起始时间)所在的value后的差值,

如果最大时间所在的value与最小时间所在的value有一个以上未找到,则该差值为0,

如起始时间为'2002-8-1',终止时间'2002-8-23' 要求的返回结果集如下:

tab1.name 差值

'a' 10
'b' 20
: :
: :
: :

注意:tab2表的数据量效大,差不多在一百万条左右,所以,不得不考虑查询效率的问题!
...全文
22 17 打赏 收藏 转发到动态 举报
写回复
用AI写文章
17 条回复
切换为时间正序
请发表友善的回复…
发表回复
qybao 2002-08-23
  • 打赏
  • 举报
回复
没有调试,试试看
select A.[name] [name],isnull(B.value-C.value,0) 差值
from tab1 A,
(select value,max([time]) from tab2 where id=A.id and [time] between '2002-8-1' and '2002-8-23' group by [time]) As B,
(select value,min([time]) from tab2 where id=A.id and [time] between '2002-8-1' and '2002-8-23' group by [time]) As C
qybao 2002-08-23
  • 打赏
  • 举报
回复
没有调试,试试看
select A.name name,isnull(B.value-C.value,0) 差值
from tab1 A,
(select value,max(time) from tab2 where id=A.id and time between mintime and maxtime group by time) As B,
(select value,min(time) from tab2 where id=A.id and time between mintime and maxtime group by time) As C
Yang_ 2002-08-23
  • 打赏
  • 举报
回复
还是别吵吧!

这样分步也许效率高些:
declare @temp table (id char(7),maxtime datetime,mintime datetime)

insert @temp
select id,max(time) as maxtime,min(time) as mintime
from tab1 where time between '2002-8-1' and '2002-8-23' group by id

select x.id,isnull(a.value,0)-isnull(b.value,0) as 差值
from @temp as x left join tab1 a
on x.id=a.id and x.maxtime=a.time
left join tab1 b
on x.id=b.id and x.mintime=b.time


自己测试一下效果,
对tab2分别加索引(id,time) or (time,id) or (id)+(time) 看那个效果最好。

如果可以接受的话,自己改成存储过程。


注:性能的调试主要靠自己多试验,而不是发很多贴,更不要用激将法,几乎所有人不喜欢用激将法的,本来下午就恢复了!

supsuccess 2002-08-23
  • 打赏
  • 举报
回复
斑竹让我来了!

打错字了:
我估计你们是同一个人,所以一起和你们说。
chx007 2002-08-23
  • 打赏
  • 举报
回复
以上几个答案我自己以前都试验过,不过效率都不是很高一般都需要查询十几秒的时间!由于我要做的是一个要实时刷新查询的系统,所以速度方面要求比较苛刻!!还请高手们指点一二!
supsuccess 2002-08-23
  • 打赏
  • 举报
回复
斑竹走了!

打错字了:
我估计你们是同一个人,所以仪器和你们说。
CSDNM 2002-08-23
  • 打赏
  • 举报
回复
打错字了:
3、本版不允许再贴子里大喊大叫,自重!


CSDNM 2002-08-23
  • 打赏
  • 举报
回复
版主来了!

zcflion(吃大白菜的鸟--菜鸟) 和 chx007(乱发吹风)

我估计你们是同一个人,所以仪器和你们说。
1、Yang_(扬帆破浪) 和Hookcjh() 给了你们答案,不管正确与否,是他们的劳动,你们应该尊重,你们试过他们的语句没有,有什么问题,出错信息是什么,或者现象是什么。
2、必须明确,所有回答是免费的,所有人有权不回答你的问题,也有权回答你的问题,只要你提交到这里。
3、本颁布允许再贴子里大喊大叫,自重!



supsuccess 2002-08-23
  • 打赏
  • 举报
回复
盗版:
select c.name,isnull(a.value,0)-isnull(b.value,0) as 差值
from
(select id,max(time) as maxtime,min(time) as mintime
from tab2 where time between '2002-8-1' and '2002-8-23' group by id) as x
join tab2 a on x.id=a.id and x.maxtime=a.time
join tab2 b on x.id=b.id and x.mintime=b.time
join tab1 c on x.id=c.id
chx007 2002-08-23
  • 打赏
  • 举报
回复
另:tab1大概只有60多条记录
zcflion 2002-08-23
  • 打赏
  • 举报
回复
OpenVMS、Yang_、duckcn、CSDNM、j9988、weixy、foolishchao、leimin、CoolSlob、IronPromises这些大鸟们,你们的分都是怎么来的,快出来看一下嘛!!让别人心服口服,不然人家以为你们的分是作弊得来的!
zcflion 2002-08-23
  • 打赏
  • 举报
回复
版主快出来看一下,怎么能躲起来呢!!??负责任点嘛!
chx007 2002-08-23
  • 打赏
  • 举报
回复
忘了说了,我用的是MS SQL SERVER 平台

并不一定非得是单纯SQL语句,也可以是存储过程等其他方式,只要速度快,甚至你要是认为表结构不合理,也可以提出修改建议!
zcflion 2002-08-23
  • 打赏
  • 举报
回复
哪位大哥能统计出,如果在一台微机(P3 800,256MB)的机子上,按上面的方法从500万条记录中找100条,要多久?
Hookcjh 2002-08-23
  • 打赏
  • 举报
回复
select a.name,COALESCE(b.value-c.value,0) from tab1 a,tab2 b,tab2 c,(select id,max(time) max_time,min(time) min_time from tab2 where time>='2002-8-1' and time<='2002-8-23' group by id) d where a.id=b.id and a.id=c.id and b.id=d.id and b.time*=d.max_time and c.time*=d.min_time
Yang_ 2002-08-23
  • 打赏
  • 举报
回复
错了:

select x.id,isnull(a.value,0)-isnull(b.value,0) as 差值
from
(
select id,max(time) as maxtime,min(time) as mintime
from tab1 where time between '2002-8-1' and '2002-8-23' group by id
) as x left join tab1 a
on x.id=a.id and x.maxtime=a.time
left join tab1 b
on x.id=b.id and x.mintime=b.time

Yang_ 2002-08-23
  • 打赏
  • 举报
回复
select x.id,isnull(a.value,0)-isnull(b.value,0) as 差值
from
(
select id,max(time) as maxtime,min(time) as mintime
from tab1 where time between '2002-8-1' and '2002-8-23' group by id
) as x left join tab1 a
on x.id=a.id and x.maxtime=a.time
left join tab1 b
on x.id=a.id and x.mintime=a.time

34,588

社区成员

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

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