gpt4 book ai didi

sql - 在 JPQL 中选择具有 group by 和 max 的行

转载 作者:行者123 更新时间:2023-12-04 01:45:16 25 4
gpt4 key购买 nike

我有以下结构。不在一张表中,只是为了简单起见:

Press       | Batch        | Start time
-------------------------------------------------
PRESS_1 | 2051 | 2017-05-18T00:00:00
PRESS_1 | 2052 | 2017-05-19T00:00:00
PRESS_2 | 2053 | 2017-05-20T00:00:00
PRESS_2 | 2054 | 2017-05-16T00:00:00
PRESS_3 | 2055 | 2017-05-12T00:00:00
PRESS_3 | 2056 | 2017-05-18T00:00:00
PRESS_3 | 2057 | 2017-05-19T00:00:00

我想获取每台打印机的最后计划批处理。结果应该是:

Press       | Batch        | Start time
-------------------------------------------------
PRESS_1 | 2052 | 2017-05-19T00:00:00
PRESS_2 | 2053 | 2017-05-20T00:00:00
PRESS_3 | 2057 | 2017-05-19T00:00:00

但是,当我按 group by 时,也想选择 batch。我有义务将批处理放入“Group By”子句中。

Select x.Press, x.Batch, max(x.StartTime)
From myTable x
Group by x.Press

这如何使用 JPQL 实现?

最佳答案

因为你想选择每次按下的最大开始时间的条目,你也可以使用下面提到的东西 -

Select x.Press, x.Batch, x.StartTime
From x
where (press,starttime) in
(select press, max(starttime)
from x group by press)

让我们试一试,请告诉我。

关于sql - 在 JPQL 中选择具有 group by 和 max 的行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44047479/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com