2014-09-19 59 views
1

我有這樣的查詢。錯誤:語法錯誤在或接近「和」

select 
    ad.escore, 
    ad.mscore, 
    round(sum(ps.cnt)/sum(n.cnt) * 100,1) as percent 
from 
(
    select 
    account_no, 
    -- 602 becomes '595-604' 
    to_char(trunc(empirica_score - 5, -1) + 5, '9999') || '-' || to_char(trunc(empirica_score - 5, -1) + 14, '9999') as escore, 
    -- 97 becomes '76-100'. Change the expression to group differently. 
    cast(((mfin_score - 1)/25) * 25 + 1 as text) || '-' || cast(((mfin_score - 1)/25) * 25 + 25 as text) as mscore 
    from account_details 
) ad 
join 
(
    select custno, count(*) as cnt 
    from paysoft_results 
    where result = 'Successful' 
    and resultdate >= '13/08/2014'  <------- HERE 
    and resultdate <= '12/19/2014'  <------- HERE 
    group by custno 
) ps on ps.custno = ad.account_no 
join 
(
    select customer_code, count(distinct start_date) as cnt 
    from naedo 
    and start_date >= '13/08/2014'  <------- HERE 
    and start_date <= '12/19/2014'  <------- HERE 
    group by customer_code 
) n on n.customer_code = ad.account_no 
group by ad.escore, ad.mscore; 

它工作完美,如果我沒有像上面安裝的日期。

如果我把日期我會得到一個錯誤ERROR: syntax error at or near "and"

任何想法,爲什麼?

UPDATE

好吧,我想我可以問一個問題,現在,所以如果我可以追加在這一個。

ERROR: date/time field value out of range: "13/08/2014"

我的查詢日期比較。什麼是正確的方法來做到這一點?

+0

莫爾納,更新可能應該是一個_new_問題,但我已經介紹了它在一邊,我原來的答覆。 – paxdiablo 2014-09-19 10:16:29

+1

不**不依賴隱式數據類型轉換。始終使用適當的日期文字,而不是字符串常量。 ''13/08/2014''是一個字符串,不是日期。您應該使用Oracle的to_date()函數:'to_date('13/08/2014','DD/MM/YYYY')或者(稍短一點)ANSI日期文字:'date'2014-08-13' '。 – 2014-09-19 12:13:18

回答

2

好,位將不起作用:

select customer_code, count(distinct start_date) as cnt 
from naedo 
and start_date >= '13/08/2014'  <------- HERE 
and start_date <= '12/19/2014'  <------- HERE 
group by ... 

因爲where子句開始與where,而不是一個and。否則,我們就都可以稱其爲and條款:-)

這將需要是:

select customer_code, count(distinct start_date) as cnt 
from naedo 
where start_date >= '13/08/2014' 
    and start_date <= '12/19/2014' 
group by ... 

您標HERE(第二段其他位,第一join條款)看起來不錯,它應該沒有錯誤地工作。


順便說一句,至少一個日期的是格式不正確。段:

and start_date >= '13/08/2014' 
and start_date <= '12/19/2014' 

要麼有十九(或十七拉丁前綴基於什麼8 UNDECIMBER的日期或12 日的,好,我甚至不知道在真正的月份已經失去了)是。

你需要弄清楚你的數據庫支持哪一個mm/dd/yyyydd/mm/yyyy,然後堅持只是之一。

既然你懷疑更新指出它抱怨13/08/2014,你可能會發現它應該被寫爲08/13/2014,在mm/dd/yyyy格式。

+0

facePalm ...是的只是注意到。 – morne 2014-09-19 09:28:31

1
select customer_code, count(distinct start_date) as cnt 
     from naedo 
     Where start_date >= '13/08/2014'  <------- HERE 
     and start_date <= '12/19/2014'  <------- HERE 
     group by customer_code 
1

「去哪兒」失蹤查詢:

" select customer_code, count(distinct start_date) as cnt 
     from naedo where 
     start_date >= '13/08/2014'  <------- HERE" 
" 
============================ 
select 
     ad.escore, 
     ad.mscore, 
     round(sum(ps.cnt)/sum(n.cnt) * 100,1) as percent 
    from 
    (
     select 
     account_no, 
     -- 602 becomes '595-604' 
     to_char(trunc(empirica_score - 5, -1) + 5, '9999') || '-' || to_char(trunc(empirica_score - 5, -1) + 14, '9999') as escore, 
     -- 97 becomes '76-100'. Change the expression to group differently. 
     cast(((mfin_score - 1)/25) * 25 + 1 as text) || '-' || cast(((mfin_score - 1)/25) * 25 + 25 as text) as mscore 
     from account_details 
    ) ad 
    join 
    (
     select custno, count(*) as cnt 
     from paysoft_results 
     where result = 'Successful' 
     and resultdate >= '13/08/2014'  <------- HERE 
     and resultdate <= '12/19/2014'  <------- HERE 
     group by custno 
    ) ps on ps.custno = ad.account_no 
    join 
    (
     select customer_code, count(distinct start_date) as cnt 
     from naedo where 
     start_date >= '13/08/2014'  <------- HERE 
     and start_date <= '12/19/2014'  <------- HERE 
     group by customer_code 
    ) n on n.customer_code = ad.account_no 
    group by ad.escore, ad.mscore; 
相關問題