是否可以在 JPA 中使用窗口函数?

Posted

技术标签:

【中文标题】是否可以在 JPA 中使用窗口函数?【英文标题】:Is it possible to use window functions in JPA? 【发布时间】:2021-04-20 04:27:47 【问题描述】:

我需要在 Spring Data Repository 中使用窗口函数,是 这可能吗?

我现在使用的是java spring maven,对JPA中的实体表分区一无所知。

我的 SQL 查询

select 
b.q_stop_id as q_stop_id,
b.q_work_day as q_work_day,
b.q_work_end as q_work_end,
b.q_mc_num as q_mc_num,
b.q_mc_name as q_mc_name,
b.Hnbn as q_hnbn,
b.q_koku as q_koku,
b.q_stop_st_time as q_stop_st_time,
b.q_stop_en_time as q_stop_en_time,
b.q_stop_time_s as q_stop_time_s,
b.q_stop_maj_code as q_stop_maj_code,
b.q_stop_major as q_stop_major,
b.q_stop_num as q_stop_num,
b.q_stop_minor as q_stop_minor,
b.q_pause_flag as q_pause_flag              
from        
(
select ROW_NUMBER() OVER (PARTITION BY a.rnk
ORDER BY a.q_stop_st_time,a.q_stop_en_time) AS RowNo,COUNT(*) OVER () as cnt,*          
from (
select DENSE_RANK() OVER(PARTITION BY proL.q_set_hnbn ORDER BY stL.q_stop_st_time,stL.q_stop_en_time DESC) as rnk,case when proL.q_set_hnbn  is not null and proL.q_set_hnbn !='' then proL.q_set_hnbn  else proL.q_hnbn end as Hnbn ,proL.q_set_hnbn as setHnbn, stL.q_stop_id as q_stop_id,wk.q_work_day as q_work_day,wk.q_work_end as q_work_end,mc.q_mc_num as q_mc_num,mc.q_mc_name as q_mc_name,proL.q_hnbn as q_hnbn,proL.q_koku as q_koku,stL.q_stop_st_time as q_stop_st_time,stL.q_stop_en_time as q_stop_en_time,stL.q_stop_time_s as q_stop_time_s,mst.q_stop_maj_code as q_stop_maj_code,mst.q_stop_major as q_stop_major,mst.q_stop_num as q_stop_num,mst.q_stop_minor as q_stop_minor,
mst.q_pause_flag as q_pause_flag  
from T_STOP_LOG stL inner join  M_stop_item mst on stL.q_stop_num=mst.q_stop_num 
inner join T_work_log wk on stL.q_work_id=wk.q_work_id 
inner join T_prod_log proL on wk.q_work_id =proL.q_work_id 
inner join M_mc mc on wk.q_mc_num=mc.q_mc_num 
left join T_malfunction_repair_log r on r.q_stop_id=stL.q_stop_id 
where wk.q_work_day >='2021-04-19 00:00:00.000' and 
wk.q_work_end<='2021-04-20 23:59:59.000' and mc.q_mc_num='1200' )a 
)b where b.RowNo=1 or  b.RowNo=2
    

【问题讨论】:

【参考方案1】:

>我通过使用 JPA nativeQuery = true 找到了解决方案

@Query(value = "select " + "b.q_stop_id as q_stop_id," + "b.q_work_day as q_work_day,"
        + "b.q_work_end as q_work_end," + "b.q_mc_num as q_mc_num," + "b.q_mc_name as q_mc_name,"
        + "b.Hnbn as q_hnbn," + "b.q_koku as q_koku," + "b.q_stop_st_time as q_stop_st_time,"
        + "b.q_stop_en_time as q_stop_en_time," + "b.q_stop_time_s as q_stop_time_s,"
        + "b.q_stop_maj_code as q_stop_maj_code," + "b.q_stop_major as q_stop_major,"
        + "b.q_stop_num as q_stop_num," + "b.q_stop_minor as q_stop_minor," + "b.q_pause_flag as q_pause_flag "
        + "from" + "(" + "select ROW_NUMBER() OVER (PARTITION BY a.rnk "
        + "ORDER BY a.q_stop_st_time,a.q_stop_en_time) AS RowNo,COUNT(*) OVER () as cnt,*   "
        + "from (select  DENSE_RANK() OVER(PARTITION BY proL.q_set_hnbn ORDER BY stL.q_stop_st_time,stL.q_stop_en_time DESC) as rnk,case when proL.q_set_hnbn  is not null and proL.q_set_hnbn !='' then proL.q_set_hnbn  else proL.q_hnbn end as Hnbn ,proL.q_set_hnbn as setHnbn,stL.q_stop_id as q_stop_id,wk.q_work_day as q_work_day,wk.q_work_end as q_work_end,mc.q_mc_num as q_mc_num,mc.q_mc_name as q_mc_name,proL.q_hnbn as q_hnbn,proL.q_koku as q_koku,stL.q_stop_st_time as q_stop_st_time,stL.q_stop_en_time as q_stop_en_time,stL.q_stop_time_s as q_stop_time_s,mst.q_stop_maj_code as q_stop_maj_code,mst.q_stop_major as q_stop_major,mst.q_stop_num as q_stop_num,mst.q_stop_minor as q_stop_minor,"
        + "mst.q_pause_flag as q_pause_flag  "
        + "from T_STOP_LOG stL inner join  M_stop_item mst on stL.q_stop_num=mst.q_stop_num "
        + "inner join T_work_log wk on stL.q_work_id=wk.q_work_id "
        + "inner join T_prod_log proL on wk.q_work_id =proL.q_work_id "
        + "inner join M_mc mc on wk.q_mc_num=mc.q_mc_num "
        + "left join T_malfunction_repair_log r on r.q_stop_id=stL.q_stop_id "
        + "where wk.q_work_day >=:workDay and wk.q_work_end<=:workEnd and mc.q_mc_num=:kiban)a "
        + ")b where b.RowNo=1 or  b.RowNo=2", nativeQuery = true)

【讨论】:

如果您使用的是 Java 13+,您可以使用 """ ... """ 文本块来避免所有字符串连接。

以上是关于是否可以在 JPA 中使用窗口函数?的主要内容,如果未能解决你的问题,请参考以下文章

如何使用vba在windowapi中使用findwindow函数定位窗口?

如何在带有 Postgres 的动态框架中使用窗口函数中的列值?

Angular - 是否可以创建AppComponent的弹出窗口?

无论窗口时间如何,都可以在Apache Flink中组合两个流

如何使用似乎忽略索引的窗口函数提高查询的性能?

在 fft 中应用窗口函数的正确方法