2016-08-31 166 views
0

我正在運行整個查詢並且一切運行正常,直到最後我得到錯誤(標題),輸出應該像下面的excel圖像。我已經嘗試保持小時爲char和num和date,但是我仍然收到錯誤。任何幫助,將不勝感激。請隨時提問並檢查工具中的代碼。由於Oracle SQL - 小時必須在0到23之間錯誤

Output_sample

with ht as( 
    select labno, birthdt, spectype, 
--to_char(
    ((dtrecv - dtcoll) 
    + (to_date(nvl(tmrecv, '0000'), 'hh24mi') 
    - to_date(nvl(tmcoll, '0000'), 'hh24mi')) 
    ) * 24 
--, 'hh24:mi') 
    as hours 
    from azmsds.sample_demog_view 
    where tmrecv is not null and tmcoll is not null and dtrecv is not null and dtcoll is not null 
), 


    secondQuery AS (
    select 
     ht.hours,  
     extract(year from ht.birthdt) as "YEAR", 
     extract (month from ht.birthdt) as "MONTH", 
     count(distinct ht.labno) as "initialDbsReceiptCount<1", 
    '~' AS EOL 
    from ht 
    where 1=1 
and ht.hours is not null  
     --and month not null 
     AND ht.birthdt >= '01-JAN-12' 
    -- and hours <= 23 
     and ht.spectype in (1,5,7) 
    --and hours > 23 
    group by extract(year from ht.birthdt), extract (month from ht.birthdt), ht.hours 
    order by extract(year from ht.birthdt) desc 
), 
    ThirdQuery AS (
    SELECT 
     sq.year, 
     sq.month, 
     sq."initialDbsReceiptCount<1", 
     sq.eol 
    FROM secondQuery sq 
    WHERE 1=1 
    --AND sq.hours <= 24 
) 
select * from ThirdQuery 

回答

0

在我看來,你有IETHER tmrecv OR tmcoll一些錯誤的數據...

如果你

select to_date('2500', 'hh24mi') from dual; 

,您會收到此錯誤。

所以我會嘗試下面的選擇開始縮小,其中的問題是

select to_date(nvl(tmrecv, '0000'), 'hh24mi') as hours 
from azmsds.sample_demog_view 
where tmrecv is not null and tmcoll is not null and dtrecv is not null and dtcoll is not null 

select to_date(nvl(tmcoll, '0000'), 'hh24mi')) as hours 
    from azmsds.sample_demog_view 
    where tmrecv is not null and tmcoll is not null and dtrecv is not null and dtcoll is not null 

一個或另一個的上面會給你的錯誤,那麼你可以檢查來自這些字段的數據來查找導致您錯誤的數據。

0

看起來你有無效的時間值。這應該有助於識別不好的。

select * from azmsds.sample_demog_view 
where 
     tmrecv not like '[0-2][0-9][0-5][0-9]' 
    or tmcoll not like '[0-2][0-9][0-5][0-9]' 
    or not tmrecv between '0000' and '2359' 
    or not tmcoll between '0000' and '2359' 
相關問題