2014-09-19 3 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

Морн, обновление, вероятно, должен был вопрос, но новым вариантом я описал его в сторону, чтобы мой первоначальный ответ. – paxdiablo

+1

Do ** не ** полагаться на неявное преобразование типа данных. Всегда используйте правильный литерал даты, а не константу строки. '' 13/08/2014'' - это строка, а не дата.Вы должны либо использовать функцию Oracle to_date(): 'to_date ('13/08/2014 ',' DD/MM/YYYY ')' или (немного короче) литерал даты ANSI: 'date' 2014-08-13 ' '. –

ответ

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/yyyy или dd/mm/yyyy ваша база данных поддерживает, а затем придерживается только этого .

Учитывая, что вы вопрос обновления заявляет, что жалуется 13/08/2014, вы обнаружите, что должно быть написано в 08/13/2014, в формате mm/dd/yyyy.

+0

лицоPalm ... да только замеченный. – morne

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; 
Смежные вопросы