表:scan_comekey   bill_code        scan_date
1     0000001          2008-8-1 20:57:00
2     0000001          2008-8-2 23:01:06
3     0000001          2008-8-2 06:01:06问题  bill_code相同  间隔时间10小时的数据!
间隔时间:第三条数据和第一条数据 ,billcode相同,时间也在10小时内(2008-8-2 06:01:06 - 2008-8-1 20:57:00  <10),所以满足条件.
查询结果应该为
key   bill_code        scan_date
1     0000001          2008-8-1 20:57:00
3     0000001          2008-8-2 06:01:06我相信已经阐述清楚了``谢谢大家踊跃回答!
这SQL语句如何实现?急用!

解决方案 »

  1.   

    scan_date 字段是什么类型的?如果是下面这种情况怎么选?
    1    0000001          2008-8-1 20:57:00
    2    0000001          2008-8-2 23:01:06
    3    0000001          2008-8-2 06:01:06
    4    0000001          2008-8-2 15:00:00是不是 1234 都要选上?个人觉得 SELECT 可能做不到,要存储过程才可以。
      

  2.   

    这个定义不明确
    比如
    key  bill_code        scan_date
    1    0000001          2008-8-1 20:57:00
    2    0000001          2008-8-2 06:01:06
    3    0000001          2008-8-2 10:01:06 要显示什么?
    注意:3和2在10小时之内,但1和3在10小时之外
      

  3.   

    试下SELECT * FROM scan_come R1, scan_come R2 WHERE  Abs(R1.scan_date-R2.scan_date)<10 AND R1.bill_code =R2.bill_code 
      

  4.   

    have a try
    select a.* from scan_come a
     where exsits select 1 from scan_come 
                  where bill_code = a.bill_code
                    and abs(scan_date - a.scan_date) < 10
      

  5.   

    不知道LZ用的是啥数据库,大概这样可以满足LZ的要求
      

  6.   


    我觉得你已经理解到我的意思了.1和3在10小时之外 可以通过order by scan_date 最新的数据和上一次的数据做对比是否10小时,就可以了
      

  7.   

    select a.* from scan_come a 
    where exsits select 1 from scan_come 
                  where bill_code = a.bill_code 
                    and abs(scan_date - a.scan_date) < 10 
    试试
      

  8.   


    哪里嵌套多了,只有一层嵌套
    exists和count的效率忘了哪个高了,可以用工具或命令跟踪一下检索路径和效率
    用count是这样的
    select a.* from scan_come a 
    where 0 < (select count(1) from scan_come 
    where bill_code = a.bill_code 
    and abs(scan_date - a.scan_date)  <= 10/24) --oracle的日期类型可以直接相减 date1-date2=两个日期相差的天数,所以改成小时的话要/24
      

  9.   


    用这个的话可能会出现重复,比如2,3和1都相差10小时的话,1就被选出2次
    这样的话出来的记过就是
    1
    1
    2
    3
    除非改成select distinct(R1.xxx)而如果用
    select a.* from scan_come a 
    where exsits select 1 from scan_come 
    where bill_code = a.bill_code 
    and abs(scan_date - a.scan_date)  <= 10/24出现的结果是
    1
    2
    3
     
      

  10.   

    楼主是指数据库吗?如果直接是数据库的话,那你试试下面的语句:SELECT first.* 
    FROM scan_come first,scan_come 
    WHERE first.bill_code = second.bill_code 44 
          AND ( first.scan_date-second.scan_date<=10 OR first.scan_date - second.scan_date<=-10);楼主,因为数据库学的不怎么样,所以,只能到这个程度了,不好意思啊!
      

  11.   

    不好意思,有点小错误
    重发一遍吧:
    SELECT first.* 
    FROM scan_come first,scan_come 
    WHERE first.bill_code = second.bill_code  
          AND ( first.scan_date-second.scan_date <=10 OR first.scan_date - second.scan_date <=-10); 
      

  12.   

    在给lz一个思路,如果不想用sql直接查询出结果,那么就用Java程序实现一下首先按照 bill_code 查询,按照scan_date order by scan_date desc (select * from XXXTable where bill_code = 'XXX' order by scan_date desc)然后取出开始的两条数据(
    Connection conn=null;
            Statement stmt=conn.createStatement();
            ResultSet rs=stmt.executeQuery("select * from XXXTable where bill_code = 'XXX' order by scan_date desc");
            int i=0;
            Date date1=null,date2=null;
            while(rs.next()){
                if(i>1) break;
                if(i==0){
                    date1=rs.getDate("scan_date");
                }else if(i==1){
                    date2=rs.getDate("scan_date");
                }
            }
            if(date1.getTime()-date2.getTime()>3600*1000){
                System.out.println("大于10小时");
            }
            conn.close();
    ),把时间转换成JAVA的Date对象,使用Date.getTime()方法返回一个整数,来判断第一个数据的Date.getTime()-第二条数据的Date.getTime()是否大于3600*1000,如果大于3600*1000这个数就说明对于同一个bill_code两次时间大于10小时。
        Connection对象根据应用自己天际
      

  13.   

    楼主,在和你确认一下
    你想显示的是不是,取相同的bill_code中时间最小的那一条,和这一条相差10个小时之内时间最大的那一条?也就是说同一个bill_code取两条,是不是这样的?
    另外就是,指想显示一个指定的bill_code还是所有的bill_code都要显示?
      

  14.   

    不用存储过程相当的费劲。如果指定了bill_code还简单一点。declare @start as datetime
    declare @result as datetimeselect @start = min(scan_date) from scan_come where bill_code = 0000001 
    select @result = max(scan_date) from scan_come where bill_code = 0000001
    and datediff(ss, @start, scan_date) < 36000
    select * from scan_come where scan_date = @start or scan_date = @result