【问题标题】:How to fix SQLException: ERROR: syntax error at or near ":" Position: 15?如何修复 SQLException:错误:“:”位置或附近的语法错误:15?
【发布时间】:2021-10-29 19:40:24
【问题描述】:

我想获取以下问题中提到的数据。

Query to retrieve count per hour and zero if none

所以据此,我在 Postgresql 中执行以下查询,所以它工作正常。

select '00:00'::time + g.h * interval '1 hour',count(sv.id) as orders from generate_series(0, 23, 1) g(h) left join paymentvirtualization.summery_virtualizer sv on extract(hour from sv.last_updated) = g.h and date_trunc('day', sv.last_updated) = '2019-10-11' group by g.h order by g.h;

但我的问题是我希望它像 Spring Data JPA 原生查询一样执行。

  @Query(
            value = "select '00:00'::time + g.h * interval '1 hour',count(sv.id) as orders from generate_series(0, 23, 1) g(h) left join paymentvirtualization.summery_virtualizer sv on extract(hour from sv.last_updated) = g.h and date_trunc('day', sv.last_updated) = '2019-10-11' group by g.h order by g.h",
            nativeQuery = true
    )
    List<Map<String, Integer>> getPaymentDistry();

如果我执行它,我会收到类似的错误

could not extract ResultSet; SQL [n/a]; nested exception is org.hibernate.exception.SQLGrammarException: could not extract ResultSet"

有人可以帮我吗?

谢谢,

【问题讨论】:

    标签: postgresql spring-boot spring-data-jpa


    【解决方案1】:

    混淆层阻塞::,因为它使用: 来引入命名参数。通常,解决方案是改用符合标准的cast() 运算符。

    select cast('00:00' as time) + g.h * interval '1 hour' ...
    

    或者使用 Laurenz 建议的 ANSI 时间语法:

    select time '00:00' + g.h * interval '1 hour' ...
    

    【讨论】:

    • @LaurenzAlbe:很好,我已经修改了答案。
    猜你喜欢
    • 2018-11-21
    • 2020-05-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-10-13
    • 1970-01-01
    • 2014-09-19
    相关资源
    最近更新 更多