如何在java spring boot中使用count和row_number

发布于 2025-01-20 20:02:55 字数 1040 浏览 0 评论 0原文

我想使用Cast和Count函数从数据库中获取JPA Spring引导中的数据,但是我的数据没有返回任何ID。因此,我创建了新的字段,该字段使用row_number()在SQL中返回自动号码。

我在JPA Spring Boot中遇到问题以使用Row_number获取数据,

这是我的SQL语法,

select row_number () over() as id,
cast (t.create_datetime as date) as reportDate,
count(*) as totalTransaction 
from vx_transaction."transaction" t
group by cast(t.create_datetime as date)

数据库中的结果如预期

导致dbeaver

但是,我在JPA Spring中遇到了错误

错误:“ T.ID”列必须通过子句出现在组中或用于 聚合功能

,我正在尝试通过在组中添加ID来解决问题。 代码

select row_number () over() as id,
cast (t.create_datetime as date) as reportDate,
count(*) as totalTransaction 
from vx_transaction."transaction" t
group by cast(t.create_datetime as date), id

这是没有错误的

,但是.....结果不如预期不符合预期的结果< /a>

任何人可以帮助我解决这个问题吗?

I want to fetch the data in JPA Spring Boot from database using cast and count function, but my data is not returning any ID. So, I creating new field that returning auto number using row_number() in SQL.

I'm getting a problem in JPA Spring Boot to fetch the data using row_number,

here is my SQL Syntax

select row_number () over() as id,
cast (t.create_datetime as date) as reportDate,
count(*) as totalTransaction 
from vx_transaction."transaction" t
group by cast(t.create_datetime as date)

the result in databases is as expected

Result in DBEAVER

but, I'm getting error in JPA Spring

ERROR: column "t.id" must appear in the GROUP BY clause or be used in
an aggregate function

Then, I'm trying to fix the problem with adding the id in group by. Here's the code

select row_number () over() as id,
cast (t.create_datetime as date) as reportDate,
count(*) as totalTransaction 
from vx_transaction."transaction" t
group by cast(t.create_datetime as date), id

There is not error, but..... the result not as expected

Not as expected result

Can anyone help me to solve this problem??

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文