【问题标题】:ERROR: syntax error at or near "and"错误:“和”处或附近的语法错误
【发布时间】:2014-09-19 09:24:24
【问题描述】:

我有一个这样的查询。

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"

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

【问题讨论】:

  • 早上,更新可能应该是一个新的问题,但我已经在我原来的答案的旁边提到了它。
  • 不要依赖隐式数据类型转换。始终使用正确的日期文字,而不是字符串常量。 '13/08/2014' 是一个字符串,而不是日期。您应该使用 Oracle 的 to_date() 函数:to_date('13/08/2014', 'DD/MM/YYYY') 或(稍微短一点)ANSI 日期文字:date '2014-08-13'

标签: sql postgresql


【解决方案1】:

好吧,这个位行不通:

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'

有一个日期是 12 月 8 日 或 12th,好吧,我什至不知道是什么知道十九的拉丁语前缀(或基于已经不同步的真实月份的十七)是。

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

鉴于您质疑更新状态它抱怨13/08/2014,您可能会发现它应该写为08/13/2014,采用mm/dd/yyyy 格式。

【讨论】:

  • facePalm ...是的,刚刚注意到。
【解决方案2】:
    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

【讨论】:

    【解决方案3】:

    查询中缺少“Where”:

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

    【讨论】:

      猜你喜欢
      • 2017-02-20
      • 2017-07-15
      • 2010-12-24
      • 2016-07-10
      • 2018-11-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多