错误:语法错误在或附近"和"

时间:2014-09-19 09:24:24

标签: sql postgresql

我有这样的查询。

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"

任何想法为什么?

更新

好吧,我想我可以问一个现在的问题,所以如果我能加上这个问题。

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

我的查询中的日期比较。这样做的正确方法是什么?

3 个答案:

答案 0 :(得分: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'

或者是Undecimber的8 th 或12 th 的日期,好吧,我甚至知道十九的拉丁语前缀(或十七个基于已经不合时宜的实际月份)是。

您需要确定数据库支持的mm/dd/yyyydd/mm/yyyy中的哪一个,然后只使用一个。

鉴于您对更新状态提出质疑,它抱怨13/08/2014,您可能会发现它应该以{{1​​}}格式写成08/13/2014

答案 1 :(得分: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

答案 2 :(得分:1)

&#34;其中&#34;查询中缺少:

"    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;