Oracle代码:select
FROM_UNIXTIME(unix_timestamp(test_time)- (unix_timestamp(test_time) mod(30*60))) as testTime,
round(avg(score),2)*0.1 as avg
from data_dns_week
where 1=1
and to_char(test_time,'yyyy-MM-dd hh24:mi:ss') >= '2011-07-30 00:00:00'
and to_char(test_time,'yyyy-MM-dd hh24:mi:ss') <= '2011-08-30 23:59:59'
group by testTime
order by testTime
MySql代码:select
FROM_UNIXTIME(unix_timestamp(test_time)- (unix_timestamp(test_time) mod(30*60))) as testTime,
round(avg(score),2)*0.1 as avg
from data_dns_week
where 1=1
and test_time >= '2011-07-30 00:00:00'
and test_time <= '2011-08-30 23:59:59'
group by testTime
order by testTime以上的Mysql代码可以正常运行 , 而Oracle代码却不能运行。 错误:ORA-00907: missing right parenthesis 这里的错误报的是代码中的mod这里 。。求大神解决下,谢谢了 ~
FROM_UNIXTIME(unix_timestamp(test_time)- (unix_timestamp(test_time) mod(30*60))) as testTime,
round(avg(score),2)*0.1 as avg
from data_dns_week
where 1=1
and to_char(test_time,'yyyy-MM-dd hh24:mi:ss') >= '2011-07-30 00:00:00'
and to_char(test_time,'yyyy-MM-dd hh24:mi:ss') <= '2011-08-30 23:59:59'
group by testTime
order by testTime
MySql代码:select
FROM_UNIXTIME(unix_timestamp(test_time)- (unix_timestamp(test_time) mod(30*60))) as testTime,
round(avg(score),2)*0.1 as avg
from data_dns_week
where 1=1
and test_time >= '2011-07-30 00:00:00'
and test_time <= '2011-08-30 23:59:59'
group by testTime
order by testTime以上的Mysql代码可以正常运行 , 而Oracle代码却不能运行。 错误:ORA-00907: missing right parenthesis 这里的错误报的是代码中的mod这里 。。求大神解决下,谢谢了 ~
我不是想知道Mod什么 意思 , 我只是想知道Mysql执行通过 , 我在Oracle中用什么函数也可以让它通过?
--mysql里面 求A/B的余数
A mod B
--oracle里面求A/B的余数
mod(A,B)--因此
select
FROM_UNIXTIME(unix_timestamp(test_time)- mod(unix_timestamp(test_time),(30*60))) as testTime,
round(avg(score),2)*0.1 as avg
from data_dns_week
where 1=1
and to_char(test_time,'yyyy-MM-dd hh24:mi:ss') >= '2011-07-30 00:00:00'
and to_char(test_time,'yyyy-MM-dd hh24:mi:ss') <= '2011-08-30 23:59:59'
group by testTime
order by testTime;
--另外oracle里面也没有 FROM_UNIXTIME、unix_timestamp函数
--UNIX_TIMESTAMP(), UNIX_TIMESTAMP(date)
--若无参数调用
----则返回一个 Unix timestamp ('1970-01-01 00:00:00' GMT 之后的秒数) 作为无符号整数。
----若用date 来调用 UNIX_TIMESTAMP(),它会将参数值以'1970-01-01 00:00:00' GMT后的秒数的形式返回。
------date 可以是一个 DATE 字符串、一个 DATETIME字符串、一个 TIMESTAMP或一个当地时间的YYMMDD 或YYYMMDD格式的数字。
--那么unix_timestamp(test_time) 转换为
test_time-to_date('1970-01-01 00:00:00','yyyy-mm-dd hh24:mi:ss')--FROM_UNIXTIME()是MySQL里的时间函数
--date为需要处理的参数(该参数是Unix 时间戳),可以是字段名,也可以直接是Unix 时间戳字符串
--后面的 '%Y%m%d' 主要是将返回值格式化
--例如:
--mysql>SELECT FROM_UNIXTIME( 1249488000, '%Y%m%d' )
--->20071120
-----因此实际上觉得就是一个格式化输出的东西,将秒转换为时间
---下面的语句试试SELECT TEST_TIME - TO_DATE('1970-01-01 00:00:00', 'yyyy-mm-dd hh24:mi:ss') +
(TEST_TIME - TO_DATE('1970-01-01 00:00:00', 'yyyy-mm-dd hh24:mi:ss') -
MOD(TEST_TIME -
TO_DATE('1970-01-01 00:00:00', 'yyyy-mm-dd hh24:mi:ss'),
(30 * 60))) / 86400 AS TESTTIME,
ROUND(AVG(SCORE), 2) * 0.1 AS AVG
FROM DATA_DNS_WEEK
WHERE 1 = 1
AND TO_CHAR(TEST_TIME, 'yyyy-MM-dd hh24:mi:ss') >= '2011-07-30 00:00:00'
AND TO_CHAR(TEST_TIME, 'yyyy-MM-dd hh24:mi:ss') <= '2011-08-30 23:59:59'
GROUP BY TESTTIME
ORDER BY TESTTIME;
时间 平均
2011-08-09 12:00:00 25
2011-08-09-12:30:00 50
.
.
.
.
等
是这种效果 , 你这个执行起来不是我要的效果啊 。
那Oracle中我可以用unix_timestamp函数得到一串数字 那我用什么函数可以得到一个由unix_timestamp函数生成的2011-02-05 12:00:00:00 这种格式的时间呢 ?
--oracle中用to_char,sysdate是系统日期,具体可以换成你的日期字段select to_char(sysdate,'yyyy-mm-dd hh24:mi:ss') from dual;