oracle如何查询一条按照周来过滤的结果集:例如查询2013年8月第2周的所有数据,
table表中有一个字段insert_time存放的是插入时间,请问sql如何写? 另外hql如何写?oracle  sql语句

解决方案 »

  1.   


    这种是很简单的了
    1、把你传入的参数,应该是一个日期吧,根据这个日期获得它一周的数据,比如说可今天是9.22,你就获取9.22-9.28的日期
    2、然后将第一步获取的日期范围传入到你的查询条件中,select * from table where insert_time between 9.22 and 9.28 像这样不就可了吗?
      

  2.   

    select * from table where insert_time between to_char(insert_time,'yyyyMMdd')='20130922' and to_char(insert_time,'yyyyMMdd')='20130928'
      

  3.   


    这种是很简单的了
    1、把你传入的参数,应该是一个日期吧,根据这个日期获得它一周的数据,比如说可今天是9.22,你就获取9.22-9.28的日期
    2、然后将第一步获取的日期范围传入到你的查询条件中,select * from table where insert_time between 9.22 and 9.28 像这样不就可了吗?oracle中有个函数to_char(字段,'w') 
      

  4.   

    月份和周数 都可以已参数的方式传进来进行查询
    with t1 as
    (
         select date'2013-08-02' c1 from dual union all
         select date'2013-08-05' c1 from dual union all
         select date'2013-08-08' c1 from dual union all
         select date'2013-08-11' c1 from dual union all
         select date'2013-08-22' c1 from dual union all
         select date'2013-08-26' c1 from dual 
    )select *
    from t1
    where to_char(c1,'w')=2
          and c1 >= to_date('2013-08','yyyy-mm') 
          and c1 < add_months(to_date('2013-08','yyyy-mm'),1)       c1
    -----------------------
    1 2013/8/8
    2 2013/8/11
      

  5.   

    select *
      from table t
     where to_char(t.insert_time, 'mm') = 8
       and to_char(t.insert_time, 'w') = 2;