oracle如何求一个月内的平均价格

march_on 2014-07-22 10:50:16
我的需求是这样的:计算2013 1月到6月之间每个月的平均收益。
我写的语句是这样的:

引用
select sum( sda.cpcconsume/100/to_char( last_day( to_date('20130101','yyyy-mm-dd') ),'dd') )
from queryman.stat_day_account sda

where sda.createdate >= to_date('20130101', 'yyyymmdd')
and sda.createdate < to_date('20130131', 'yyyymmdd')
having (sum(sda.cpcconsume) / 100 )>=500




我不知道怎么一下子计算出6各月的数据,所以先算1月份的。结果执行的时候提示算数运算导致溢出。问题出在计算每个月多少天那里 了,我把
引用
to_char( last_day( to_date('20130101','yyyy-mm-dd') ),'dd')
换成31还是同样的错误,但是换成30后却是正常的,求教应该怎么修改呢?
...全文
899 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
alex_only 2014-08-16
  • 打赏
  • 举报
回复
你把createdate 字段转成YYYYMM6位,然后根据这个6位的字段分组求不就行了么
CT_LXL 2014-07-23
  • 打赏
  • 举报
回复
引用 5 楼 march_on 的回复:
[quote=引用 3 楼 zlloct 的回复:] [quote=引用 楼主 march_on 的回复:] 我的需求是这样的:计算2013 1月到6月之间每个月的平均收益。 我写的语句是这样的:
引用
select sum( sda.cpcconsume/100/to_char( last_day( to_date('20130101','yyyy-mm-dd') ),'dd') ) from queryman.stat_day_account sda where sda.createdate >= to_date('20130101', 'yyyymmdd') and sda.createdate < to_date('20130131', 'yyyymmdd') having (sum(sda.cpcconsume) / 100 )>=500
我不知道怎么一下子计算出6各月的数据,所以先算1月份的。结果执行的时候提示算数运算导致溢出。问题出在计算每个月多少天那里 了,我把
引用
to_char( last_day( to_date('20130101','yyyy-mm-dd') ),'dd')
换成31还是同样的错误,但是换成30后却是正常的,求教应该怎么修改呢?
select sum(sda.cpcconsume / 100 / (to_date('20140630', 'yyyymmdd') -
           to_date('20140101', 'yyyymmdd') + 1))
  from queryman.stat_day_account sda
 where sda.createdate >= to_date('20130101', 'yyyymmdd')
   and sda.createdate <= to_date('20130630', 'yyyymmdd')
 having(sum(sda.cpcconsume) / 100) >= 500;
[/quote] 多谢帮助。我想要计算每个月的平均收益,你的答案计算的是这6各月的平均收益吧[/quote] 你是在其他程序中调用报错还是在SQL执行的时候报错?
卖水果的net 2014-07-23
  • 打赏
  • 举报
回复
引用 4 楼 march_on 的回复:
[quote=引用 2 楼 wmxcn2000 的回复:]

-- LZ 执行下面的语句,也出错吗?
select to_char(      last_day(        to_date('20130101','yyyy-mm-dd') ),'dd')  from dual 
没有,这个没出错,结果是31[/quote] LZ ,方便截图吗?看图比较直观。
march_on 2014-07-23
  • 打赏
  • 举报
回复
引用 3 楼 zlloct 的回复:
[quote=引用 楼主 march_on 的回复:] 我的需求是这样的:计算2013 1月到6月之间每个月的平均收益。 我写的语句是这样的:
引用
select sum( sda.cpcconsume/100/to_char( last_day( to_date('20130101','yyyy-mm-dd') ),'dd') ) from queryman.stat_day_account sda where sda.createdate >= to_date('20130101', 'yyyymmdd') and sda.createdate < to_date('20130131', 'yyyymmdd') having (sum(sda.cpcconsume) / 100 )>=500
我不知道怎么一下子计算出6各月的数据,所以先算1月份的。结果执行的时候提示算数运算导致溢出。问题出在计算每个月多少天那里 了,我把
引用
to_char( last_day( to_date('20130101','yyyy-mm-dd') ),'dd')
换成31还是同样的错误,但是换成30后却是正常的,求教应该怎么修改呢?
select sum(sda.cpcconsume / 100 / (to_date('20140630', 'yyyymmdd') -
           to_date('20140101', 'yyyymmdd') + 1))
  from queryman.stat_day_account sda
 where sda.createdate >= to_date('20130101', 'yyyymmdd')
   and sda.createdate <= to_date('20130630', 'yyyymmdd')
 having(sum(sda.cpcconsume) / 100) >= 500;
[/quote] 多谢帮助。我想要计算每个月的平均收益,你的答案计算的是这6各月的平均收益吧
march_on 2014-07-23
  • 打赏
  • 举报
回复
引用 2 楼 wmxcn2000 的回复:

-- LZ 执行下面的语句,也出错吗?
select to_char(      last_day(        to_date('20130101','yyyy-mm-dd') ),'dd')  from dual 
没有,这个没出错,结果是31
CT_LXL 2014-07-23
  • 打赏
  • 举报
回复
引用 楼主 march_on 的回复:
我的需求是这样的:计算2013 1月到6月之间每个月的平均收益。 我写的语句是这样的:
引用
select sum( sda.cpcconsume/100/to_char( last_day( to_date('20130101','yyyy-mm-dd') ),'dd') ) from queryman.stat_day_account sda where sda.createdate >= to_date('20130101', 'yyyymmdd') and sda.createdate < to_date('20130131', 'yyyymmdd') having (sum(sda.cpcconsume) / 100 )>=500
我不知道怎么一下子计算出6各月的数据,所以先算1月份的。结果执行的时候提示算数运算导致溢出。问题出在计算每个月多少天那里 了,我把
引用
to_char( last_day( to_date('20130101','yyyy-mm-dd') ),'dd')
换成31还是同样的错误,但是换成30后却是正常的,求教应该怎么修改呢?
select sum(sda.cpcconsume / 100 / (to_date('20140630', 'yyyymmdd') -
           to_date('20140101', 'yyyymmdd') + 1))
  from queryman.stat_day_account sda
 where sda.createdate >= to_date('20130101', 'yyyymmdd')
   and sda.createdate <= to_date('20130630', 'yyyymmdd')
 having(sum(sda.cpcconsume) / 100) >= 500;
卖水果的net 2014-07-23
  • 打赏
  • 举报
回复

-- LZ 执行下面的语句,也出错吗?
select to_char(      last_day(        to_date('20130101','yyyy-mm-dd') ),'dd')  from dual 
bw555 2014-07-22
  • 打赏
  • 举报
回复
select to_char(sda.createdate,'yyyymm') 月份,
sum(sda.cpcconsume)/100/to_number(to_char(last_day( to_date(to_char(sda.createdate,'yyyymm') ||'01','yyyymmdd') ),'dd'))
from queryman.stat_day_account sda 
group by to_char(sda.createdate,'yyyymm')

17,075

社区成员

发帖
与我相关
我的任务
社区描述
Oracle开发相关技术讨论
社区管理员
  • 开发
  • Lucifer三思而后行
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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