CSDN首页 空间 新闻 论坛 Blog 下载 读书 网摘 搜索 .NET Java 视频 接项目 求职 在线学习 买书 程序员 通知
山寨机中的战斗机! 程序优化工程师到底对IT界有没有贡献
CSDN社区
搜索 收藏 打印 关闭
CSDN社区 >  MS-SQL Server >  基础类

求一句SQL

楼主hongbo163(正确的学习方法很重要)2005-02-01 12:04:33 在 MS-SQL Server / 基础类 提问

表的结构  
  ID           Speed  
  1 0  
  2 2  
  3 3  
  4 1  
  5 23  
  6 60  
  7 2  
  8 0  
  9 2  
  10 2  
  11 89  
  12 2  
  13 3  
  ..........  
   
  给定一个指定的Speed,如4  
  检索出所有Speed<4的记录,以及,紧接着Speed<4的记录段的下一条>4的记录  
  如例子,是要找出ID为:  
  1、2、3、4   和5  
  7、8、9、10   和11  
  12、13  
   
  实际的需求是计算车辆的持续低速行驶的时间  
  如,定义speed<4为低速,然后找出低速行驶的时间段  
  以及到恢复正常速度,期间持续了多长时间的低速行驶  
   
  不知道说明白了没有  
  问题点数:100、回复次数:14Top

1 楼pbsql(风云)回复于 2005-02-01 12:12:57 得分 0

select   *   from   t   where   Speed<4  
  union   all  
  select   *   from   t   a   where   Speed>=4  
      and   (select   top   1   Speed   from   t   where   id<a.id   order   by   id   desc)<4Top

2 楼jinjazz(近身剪)回复于 2005-02-01 12:14:07 得分 0

select   *   from   你的表  
  where   speed<=  
  (select   top   1   speed   from   你的表   where   speed>4   order   by   speed)  
  order   by   speedTop

3 楼pbsql(风云)回复于 2005-02-01 12:21:45 得分 45

改一下:  
  select   *   from   t   where   Speed<4  
  union   all  
  select   *   from   t   a  
    where   Speed>=4   and   exists(select   *   from   t   b   where   speed<4   and   id<a.id  
                                          and   not   exists(select   *   from   t   where   id>b.id   and   id<a.id))  
  order   by   idTop

4 楼libin_ftsafe(子陌红尘:TS for Banking Card)回复于 2005-02-01 13:00:54 得分 45

select   ID   from   t   where   a.speed   <   4  
  union   all  
  select   distinct   c.ID  
  from  
        (select   min(b.ID)   as   ID    
          from   t   a,t   b    
          where   a.ID   <   b.ID   and   a.speed   <   4   and   b.speed   >=4   group   by   a.ID)   c  
  order   by   IDTop

5 楼Qihua_wu(小吴)回复于 2005-02-01 13:15:47 得分 0

select   *   from   表   where   speed   <=指定值  
  union  
  select   *   from   表   where   id   =   (select   top   1   id   from   表   where   speed>指定值   order   by   asc)Top

6 楼Qihua_wu(小吴)回复于 2005-02-01 13:16:24 得分 0

select   *   from   表   where   speed   <=指定值  
  union  
  select   *   from   表   where   id   =   (select   top   1   id   from   表   where   speed>指定值   order   by   desc   asc)  
  Top

7 楼hglhyy(為人民币服务!)回复于 2005-02-01 14:35:30 得分 0

declare   @sp   int  
   
  set   @sp=4  
   
  select   *   into   #s   from   U_table   where   speed>@sp  
   
  select   a.id,a.Speed   from   U_table   a   where   a.Speed<@sp   and   not   exist  
  (select   *   from   #s   b   where   a.id=b.id   and   min(b.id)>a.id)    
   
   
  drop   table   #sTop

8 楼wangzhanlili(小木头)回复于 2005-02-01 14:50:11 得分 0

select   *   from   t   where   speed   <=4   union  
  select   *   from   t   where   id   =   (select   top   1   id   from   表   where   speed>4   order   by   desc   asc)  
  Top

9 楼hongbo163(正确的学习方法很重要)回复于 2005-02-01 16:04:27 得分 0

测试过pbsql(风云)的方案,是正确的  
  正在学习中Top

10 楼libin_ftsafe(子陌红尘:TS for Banking Card)回复于 2005-02-01 16:12:29 得分 0

修正一下,之前第一行多写了一个"a."  
  -------------------------------------------------------------------------  
  select   ID   from   t   where   speed   <   4  
  union   all  
  select   distinct   c.ID  
  from  
        (select   min(b.ID)   as   ID    
          from   t   a,t   b    
          where   a.ID   <   b.ID   and   a.speed   <   4   and   b.speed   >=4   group   by   a.ID)   c  
  order   by   ID  
  Top

11 楼hongbo163(正确的学习方法很重要)回复于 2005-02-01 16:16:45 得分 0

楼上也测试过,也是正确的Top

12 楼hongbo163(正确的学习方法很重要)回复于 2005-02-01 16:17:30 得分 0

谁能帮忙解释一下pbsql(风云)的SQL语句  
   
  我看的糊涂了Top

13 楼jinjazz(近身剪)回复于 2005-02-01 16:19:58 得分 10

我觉得我的也没错-_-!!Top

14 楼hongbo163(正确的学习方法很重要)回复于 2005-02-01 16:27:49 得分 0

to   jinjazz(近身剪(N-P攻略))    
   
  你这里理解错了  
  select   top   1   speed   from   你的表   where   speed>4   order   by   speed  
   
  1 0  
  2 2  
  3 3  
  4 1  
  5 23           这里是一个低速段,以ID=5这个正常速度(23公里/小时)结束  
                                  计算1到5的时间间隔可以知道低速行驶了多久    
   
  7 2  
  8 0  
  9 2  
  10 2  
  11 89         这里是一个低速段,以ID=11这个正常速度(89公里/小时)结束  
                                  计算7到11的时间间隔可以知道低速行驶了多久    
   
   
  12 2  
  13 3Top

相关问题

  • 一句sql
  • 一句sql
  • 一句sql
  • 一句sql
  • 求一句SQL。
  • 求一句SQL
  • 求一句SQL
  • 一SQL语句?
  • 一句 sql !
  • 求一句sql

关键词

  • speed
  • 行驶
  • 计算
  • 时间
  • where

得分解答快速导航

  • 帖主:hongbo163
  • pbsql
  • libin_ftsafe
  • jinjazz

相关链接

  • SQL Server类图书

广告也精彩

反馈

请通过下述方式给我们反馈
反馈
提问
网站简介|广告服务|VIP资费标准|银行汇款帐号|网站地图|帮助|联系方式|诚聘英才|English|问题报告
北京创新乐知广告有限公司 版权所有, 京 ICP 证 070598 号
世纪乐知(北京)网络技术有限公司 提供技术支持
Copyright © 2000-2008, CSDN.NET, All Rights Reserved
GongshangLogo