2016-04-08 19 views
1

我不是交易程序員,但我知道一些SQL。我只需要另外一組代碼,因爲我不確定爲什麼會出現此錯誤。使用日期的ORA-01858錯誤

select 
count(1) AH 
      from regmdr.contact_interaction ci 
      join regmdr.source_data sd on ci.sd_id = sd.sd_id 
      join regmdr.account_contact ac on ci.acct_cont_id = ac.acct_cont_id 
      join regmdr.account acc on ac.acc_id = acc.acc_id 
      where sd.sd_user_type in ('1','2') 
      and sd.sd_origin_reference = 'www.alliancehealth.com' 
      and ci.ci_create_date in (select case when ci.ci_create_date between to_date('1/1/2016','mm/dd/yyyy') and to_date('1/31/2016','mm/dd/yyyy') then 'January' 
              when ci.ci_create_date between to_date('2/1/2016','mm/dd/yyyy') and to_date('2/29/2016','mm/dd/yyyy') then 'February' 
              when ci.ci_create_date between to_date('3/1/2016','mm/dd/yyyy') and to_date('3/31/2016','mm/dd/yyyy') then 'March' 
        else '' end from regmdr.contact_interaction ci   group by to_char(ci.ci_create_date, 'yyyy-mm') 
+0

請編輯您的帖子並使用源代碼標記以便查詢可讀。 –

+0

乍一看,這是因爲你正在比較'1月','2月','3月'的ci.ci_create_date(這是一個日期)。還有其他問題 - 你正在按ci.ci_create_date =>'YYYYMM'分組,但它不是你選擇的一部分。 –

回答

3

您正在試圖比較由你的case語句,這是一月,二月,三月或空生成的字符串的ci.ci_create_date日期值。所以你有效地做這樣的比較:

ci.ci_create_date = to_date('January') 

除非你的NLS_DATE_FORMAT是「月」,你的語言是英語,這將得到ORA-01858。您可能也將其左側轉換爲月份名稱,但沒有包含任何年份一月份數據的格式模型中的年份;如果可以避免的話,最好不要從表格中轉換數據。

這並不完全清楚你要做什麼,但作爲子查詢有一個獨立的contact_interaction視圖沒有關聯,它可能不會做任何你試圖做任何事情。

如果你想從第三個月今年的計數值,那麼你可以這樣做:

select count(1) AH 
from regmdr.contact_interaction ci 
join regmdr.source_data sd on ci.sd_id = sd.sd_id 
join regmdr.account_contact ac on ci.acct_cont_id = ac.acct_cont_id 
join regmdr.account acc on ac.acc_id = acc.acc_id 
where sd.sd_user_type in ('1','2') 
and sd.sd_origin_reference = 'www.alliancehealth.com' 
and ci.ci_create_date >= date '2016-01-01' 
and ci.ci_create_date < date '2016-04-01' 

,這將給你一個數字。如果您每月希望你group by是在錯誤的地方,你可以添加

... 
group by trunc(ci.ci_create_date, 'MM') 

雖然你真正需要的是在選擇列表中也對結果集進行任何意義 - 這樣你就知道哪個月份各計數屬於。

基於使用的月份名稱可言,也許你想那些在選擇列表:

select to_char(trunc(ci.ci_created_date, 'MM'), 'Month') as month, 
    count(1) as AH 
from regmdr.contact_interaction ci 
... 
group by trunc(ci.ci_create_date, 'MM') 

...但更我現在猜測。另請注意,月份名稱對您的NLS設置很敏感,特別是NLS_DATE_LANGUAGE。儘管如此,你可以強迫他們始終使用英文via the optional third argument to to_char()

select to_char(trunc(ci.ci_created_date, 'MM'), 'Month', 'NLS_DATE_LANGUAGE=ENGLISH') 
... 
+0

你可能是對的,但是看起來你正在讀一年的公式,而她的原始代碼並不是這樣。她正在剝離一年。這看起來有點像她不明白她的代碼是這樣做的,或者她想要查看每年前三個月的數據。 –

+0

@TGray - 子查詢中的情況限制了一年,當結果用於比較時,它是無效的,所以我認爲這不是一個很大的飛躍。我確實說「如果」,因爲不清楚真正需要什麼。再看一遍,我想知道月份名稱是打算在選擇列表中(沒有年份),通過這種情況... –

+0

並不意味着聽起來過於關鍵。只是有點困惑,她要求什麼。 –

0

爲什麼不一起去:

select to_char(ci.ci_create_date, 'YYYY-MM') monthyear, 
     count(1) AH 
from regmdr.contact_interaction ci 
join regmdr.source_data sd on ci.sd_id = sd.sd_id 
join regmdr.account_contact ac on ci.acct_cont_id = ac.acct_cont_id 
join regmdr.account acc on ac.acc_id = acc.acc_id 
where sd.sd_user_type in ('1','2') 
and sd.sd_origin_reference = 'www.alliancehealth.com' 
and to_char(ci.ci_create_date, 'MON' in ('JAN', 'FEB', 'MAR') 
group by to_char(ci.ci_create_date, 'yyyy-mm'; 

如果你只在計數感興趣的(沒有上下文),把它包在外面的SELECT語句:

select AH 
from (select to_char(ci.ci_create_date, 'YYYY-MM') monthyear, 
      count(1) AH 
     from regmdr.contact_interaction ci 
     join regmdr.source_data sd on ci.sd_id = sd.sd_id 
     join regmdr.account_contact ac on ci.acct_cont_id = ac.acct_cont_id 
     join regmdr.account acc on ac.acc_id = acc.acc_id 
     where sd.sd_user_type in ('1','2') 
     and sd.sd_origin_reference = 'www.alliancehealth.com' 
     and to_char(ci.ci_create_date, 'MON') in ('JAN', 'FEB', 'MAR') 
     group by to_char(ci.ci_create_date, 'yyyy-mm' 
); 
+0

謝謝,我會嘗試你的建議,看看會發生什麼! – Michelle

+1

比較月份名稱時,絕對不要依賴於匹配當前會話設置,而是明確命名月份名稱的語言:'(JAN','FEB',' 'MAR')'。 –