繁体   English   中英

在jOOQ中的DISTINCT ON()

[英]DISTINCT ON() in jOOQ

我想在PostgreSQL中进行查询

select 
  distinct on(uuid) 
  (select nr_zew from bo_get_sip_cti_polaczenie_info(uuid)) as nr_zew 
from bo_sip_cti_event_day
where data_ins::date = current_date
and kierunek like 'P'
and (hangup_cause like 'NO_ANSWER' or hangup_cause like 'NO_USER_RESPONSE') 

在Java中,我有

Result<Record> result = create
    .select()
    .from("bo_sip_cti_event_day")
    .where("data_ins::date = current_date")
    .and("kierunek like 'P'")
    .and("(hangup_cause like 'NO_ANSWER' or hangup_cause like 'NO_USER_RESPONSE') ")
    .fetch();

它工作,但我试图添加

Result<Record> result = create
    .selectDistinct("uuid")
    .from("bo_sip_cti_event_day")
    .where("data_ins::date = current_date")
    .and("kierunek like 'P'")
    .and("(hangup_cause like 'NO_ANSWER' or hangup_cause like 'NO_USER_RESPONSE') ")
    .fetch();

然后它说不能做selectDistinct(String). 如何在jOOQ中使用distinct?

发现这绝对不是很明显。 选择实际列后,有一个SelectDistinctOnStep.distinctOn()方法。 之所以发现它是不明显的,那就是PostgreSQL语法本身在像jOOQ这样的内部DSL中很难建模。

您可以这样考虑:您正在选择一组列(相关子查询),同时指定应在哪些列上应用清晰度过滤器:

Result<Record> result = create
  .select(field("(select nr_zew from bo_get_sip_cti_polaczenie_info(uuid))").as("nr_zew"))
  .distinctOn(field("uuid"))
  .from("bo_sip_cti_event_day")
  .where("data_ins::date = current_date")
  .and("kierunek like 'P'")
  .and("(hangup_cause like 'NO_ANSWER' or hangup_cause like 'NO_USER_RESPONSE') ")
  .fetch();

或者,如果您使用代码生成器:

Result<Record> result = create
  .select(field(
     select(BO_GET_SIP_CTI_POLACZENIE_INFO.NR_ZEW)
    .from(BO_GET_SIP_CTI_POLACZENIE_INFO.call(BO_SIP_CTI_EVENT_DAY.UUID))).as("nr_zew"))
  .distinctOn(BO_SIP_CTI_EVENT_DAY.UUID)
  .from(BO_SIP_CTI_EVENT_DAY)
  .where(BO_SIP_CTI_EVENT_DAY.cast(Date.class).eq(currentDate()))
  .and(BO_SIP_CTI_EVENT_DAY.KIERUNEK.like("P"))
  .and(BO_SIP_CTI_EVENT_DAY.HANGUP_CAUSE.like("NO_ANSWER")
    .or(BO_SIP_CTI_EVENT_DAY.HANGUP_CAUSE.like("NO_USER_RESPONSE")))
  .fetch();

关于你对LIKE的使用的侧面说明

请注意,下划线( _ )字符是SQL中的单字符通配符,因此您的LIKE谓词可能不完全正确。 理想情况下,只需使用普通的比较谓词,例如:

  • kierunek = 'P'
  • hangup_cause IN ('NO_ANSWER', 'NO_USER_RESPONSE')

你真的不需要LIKE

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM