解决方案 »
- 用语句在Oracle SQL*Plus 把表导成 execl
- 数据插入问题
- 急!sql语句怎么写(update语句)???
- 送分:以下两句SQL,哪一句更快?
- 大虾,数据库复制的问题,满送100分
- dbms_job执行为什么每天都往后推迟30分钟? 我的interval = sysdate + 1
- vc开发数据库时,使用addnew在数据库里增加纪录不能通过,请问为什么?
- case when 可不可以判斷某字段是否為空。如:case when bookqty1 is null then bookqty1 else bookqty2
- 不同数据库,不同表结构迁移
- PL/SQL
- 操作系统损坏时ORACLE如何恢复
- sql问题,这个能不能通过sql解决
伪码:
select case when ontime < 08:00 then 08:00
when ontime > 22:00 then trunc(ontime)+1 08:00
else ontime end ontime,
case when offtime < 08:00 then trunc(offtime)-1 22:00
when offtime > 22:00 then 22:00
else offtime end offtime
from t1;外层嵌套相减,除去夜间10小时
如第三条记录,
2009-09-04 23:00 2009-09-07 07:00 14小时 开机时间以次日8点算,关机时间以前一日22点算5日8时到6日8时即14小时,7日7时关机,算做6日22时关机,6日8时到6日22时又14个小时,应该是28个小时。 select decode(sign(c),1,c*24-trunc(c)*10,0) from
(
select
( case when to_char(offtime,'hh24') <'08' then trunc(offtime,'dd')-1+22/24
when to_char(offtime,'hh24') > '22' then trunc(offtime,'dd')+22/24
else offtime
end -
case when to_char(ontime,'hh24') <'08' then trunc(ontime,'dd')+1/3
when to_char(ontime,'hh24') > '22' then trunc(ontime,'dd')+1+1/3
else ontime
end
) c
from tt )
SELECT ontime,
offtime,
case
when adj_offtime - adj_ontome < 0 then
0
else
24 * (adj_offtime - adj_ontome)
end - 10 * case
when (adj_offtime - adj_ontome) < 0 then
0
else
floor(adj_offtime - adj_ontome)
end acthours
FROM (select t.offtime,
t.ontime,
case
when to_number(to_char(t.offtime, 'HH24')) < 8 then
TO_DATE(to_char(t.offtime - 1, 'YYYY-MM-DD') || ':22',
'YYYY-MM-DD HH24')
when to_number(to_char(t.offtime, 'HH24')) > 22 then
TO_DATE(to_char(t.offtime - 1, 'YYYY-MM-DD') || ':22',
'YYYY-MM-DD HH24')
else
t.offtime
end adj_offtime,
case
when to_number(to_char(t.ontime, 'HH24')) < 8 then
TO_DATE(to_char(t.ontime, 'YYYY-MM-DD') || ':08',
'YYYY-MM-DD HH24')
when to_number(to_char(t.ontime, 'HH24')) > 22 then
TO_DATE(to_char(t.ontime + 1, 'YYYY-MM-DD') || ':08',
'YYYY-MM-DD HH24')
else
t.ontime
end adj_ontome
from timetest t) T
round(case when to_char(ontime,'hh24mi')>to_char(offtime,'hh24mi')
then (case when to_char(ontime,'hh24mi')<='0800' or to_char(offtime,'hh24mi')>='2200'
or to_char(ontime,'hh24mi')>'2200' and to_char(offtime,'hh24mi')<='0800'
then trunc(offtime-ontime)*14/24
when to_char(ontime,'hh24mi')<='2200' and to_char(offtime,'hh24mi')<='0800'
then trunc(offtime-ontime)*14/24+trunc(ontime)+22/24-ontime
when to_char(ontime,'hh24mi')<='2200' and to_char(offtime,'hh24mi')>'0800'
then trunc(offtime-ontime+1)*14/24+trunc(ontime)-ontime+offtime-trunc(offtime)
when to_char(ontime,'hh24mi')>'2200' and to_char(offtime,'hh24mi')<'2200'
then trunc(offtime-ontime+1)*14/24+offtime-trunc(offtime)-8/24 end)
else (case when to_char(offtime,'hh24mi')<='0800' or to_char(ontime,'hh24mi')>='2200'
then trunc(offtime-ontime)*14/24
when to_char(offtime,'hh24mi')<='2200' and to_char(ontime,'hh24mi')<='0800'
then trunc(offtime-ontime)*14/24+offtime-trunc(offtime)-8/24
when to_char(offtime,'hh24mi')<='2200' and to_char(ontime,'hh24mi')>'0800'
then trunc(offtime-ontime)*14/24+mod(offtime-ontime,1)
when to_char(offtime,'hh24mi')>'2200' and to_char(ontime,'hh24mi')<='0800'
then trunc(offtime-ontime+1)*14/24
when to_char(offtime,'hh24mi')>'2200' and to_char(ontime,'hh24mi')<'2200'
then trunc(offtime-ontime+1)*14/24+trunc(ontime)+22/24-ontime end)
end*24)||'小时' times from t1
(ontime date,
offtime date)
select (offtime - ontime) * 24 - dd
from (
select decode(sign(to_char(ontime, 'hh24') - '08'),
-1,
ontime + (8 - to_char(ontime, 'hh24')) / 24,
decode(sign(to_char(ontime, 'hh24') - '22'),
-1,
ontime,
ontime + (22 - to_char(ontime, 'hh24') + 10) / 24)) ontime,
decode(sign(to_char(offtime, 'hh24') - '22'),
-1,
decode(sign(to_char(offtime, 'hh24') - '08'),
-1,
offtime + (8 - to_char(offtime, 'hh24')) / 24,
offtime),
offtime - (to_char(offtime, 'hh24') - 22) / 24) offtime,
floor(offtime - ontime) * 10 dd
from testtime) aa