当前位置: 代码迷 >> Oracle管理 >> 怎么查询指定日期 和 这个日期 之前一个月前的数据
  详细解决方案

怎么查询指定日期 和 这个日期 之前一个月前的数据

热度:128   发布时间:2016-04-24 04:12:17.0
如何查询指定日期 和 这个日期 之前一个月前的数据
本帖最后由 skyxuyan 于 2014-10-22 22:25:11 编辑
如何 用SQL 语句    查询指定日期 如(2014-10-21 10:32:39)     和  这个日期 之前一个月前的数据

指定日期  为数据表中  时间最小的  数据  

还有  是  指定的日期   不是   sysdate  

我原来写的  



select sum(ttmoney), t.pk_customer, add_months( to_date(min(t.ordertime),'yyyy-MM-dd HH24:mi:ss'), -1)

 from table  t  

 where t.ordertime  between  to_char( add_months(sysdate, -1),'yyyy-mm-dd hh24:mi:ss')
and (select  min( t.ordertime) from table )
 
  group by t.pk_customer

  

得到的数据不对  ,求帮忙!
------解决思路----------------------
select sum(ttmoney), t.pk_customer, add_months( to_date(min(t.ordertime),'yyyy-MM-dd HH24:mi:ss'), -1)
 from table  t ,(select  min( t.ordertime) D from table) A
 where t.ordertime  between  add_months(A.D, -1) and A.D
  group by t.pk_customer

------解决思路----------------------
你的ordertime类型是char型的吗?
char型需要to_date to_char进行转化一下
select sum(ttmoney), t.pk_customer, add_months( to_date(min(t.ordertime),'yyyy-MM-dd HH24:mi:ss'), -1)
from table  t ,(select  min( t.ordertime) D from table) A
where t.ordertime  between  to_char(add_months( to_date(A.D,'yyyy-MM-dd HH24:mi:ss'), -1),'yyyy-MM-dd HH24:mi:ss') and A.D
group by t.pk_customer

------解决思路----------------------
好像少点啥,指定日期不是sysdate
select sum(ttmoney), t.pk_customer, add_months( to_date(min(t.ordertime),'yyyy-MM-dd HH24:mi:ss'), -1)
from table  t ,(select  min( ordertime) D from table
                         where  ordertime<>sysdate ) A
where t.ordertime  between  
   to_char(add_months( to_date(A.D,'yyyy-MM-dd HH24:mi:ss'), -1),'yyyy-MM-dd HH24:mi:ss')  and A.D
group by t.pk_customer
  相关解决方案