作者热门文章
- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
我想在 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
。
关于java - jOOQ 中的 DISTINCT ON(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49152718/
我是一名优秀的程序员,十分优秀!