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

兔子侠客 2014-10-22 10:24:20
如何 用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



得到的数据不对 ,求帮忙!
...全文
1285 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
小灰狼W 2014-10-23
  • 打赏
  • 举报
回复
between xxx and min? <=表中的最小时间,得不到多少数据吧
bw555 2014-10-22
  • 打赏
  • 举报
回复
引用 3 楼 skyxuyan 的回复:
是字符串 是这种 2014-10-21 10:32:39 你的报 文字 与字符格式 不匹配了
检查你的数据是否有不满足此掩码的数据,正常2014-10-21 10:32:39这样的数据掩码就是yyyy-MM-dd HH24:mi:ss
不写代码的钦 2014-10-22
  • 打赏
  • 举报
回复
好像少点啥,指定日期不是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
兔子侠客 2014-10-22
  • 打赏
  • 举报
回复
引用 2 楼 bw555 的回复:
你的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
是字符串 是这种 2014-10-21 10:32:39 你的报 文字 与字符格式 不匹配了
bw555 2014-10-22
  • 打赏
  • 举报
回复
你的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
bw555 2014-10-22
  • 打赏
  • 举报
回复
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

17,377

社区成员

发帖
与我相关
我的任务
社区描述
Oracle 基础和管理
社区管理员
  • 基础和管理社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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