具有多个数组的不需要的Jooq表示法

时间:2017-05-29 06:28:53

标签: java postgresql jooq unnest

我希望将每个位置的外部时区信息混合,以过滤具有时间戳而没有时区值的行。

我如何在Jooq中表达这个SQL(Postgresql)片段

WITH 
    now(locaton, value) AS (
        SELECT 
            * 
        FROM unnest(
            ARRAY[1,2]::BIGINT[],
            ARRAY['2017-05-28 09:30','2017-05-28 10:30']::TIMESTAMP[])
    ) 
SELECT * 
FROM now;

不太令人满意但仍在工作的片段是:

WITH 
    now(locaton, value) AS (
        SELECT 
            unnest(ARRAY[1,2]::BIGINT[]) as location,
            unnest(ARRAY['2017-05-28 09:30','2017-05-28 10:30']::TIMESTAMP[]) as value
    ) 
SELECT * 
FROM now;

可能更正确(被Add support for the SQL Standard WITH ORDINALITY clause #5799阻止)

SELECT location, value
FROM 
  unnest(ARRAY[1,2]::BIGINT[]) WITH ORDINALITY as location
  JOIN unnest(ARRAY['2017-05-28 09:30','2017-05-28 10:30']::TIMESTAMP[]) WITH ORDINALITY as value 
  USING (ordinality);

获得

location    |        value
------------+---------------------
         1  | 2017-05-28 09:30:00
         2  | 2017-05-28 10:30:00
(2 rows)

1 个答案:

答案 0 :(得分:1)

好吧,我用纯SQL解决了它

DSL
    .with("now", "location", "value")
    .as(
        create
            .select(
                DSL.field("location"),
                DSL.field("value")
            )
            .from(
                DSL
                    .table(
                        "UNNEST(?::BIGINT[]) WITH ORDINALITY",
                        DSL.val(Arrays.asList(1L, 2L).toArray())
                    )
                    .as("location")
                    .join(
                        DSL
                            .table(
                                "UNNEST(?::TIMESTAMP[]) WITH ORDINALITY",
                                DSL.val(
                                    Arrays.asList(
                                        LocalDateTime.of(2017, 05, 28, 9, 30),
                                        LocalDateTime.of(2017, 05, 28, 10, 30)
                                    ).toArray()
                                )
                            )
                            .as("value")
                    )
                    .using(
                        DSL.field("ordinality")
                    )
            )
    )
    .select()
    .from(DSL.table("now"));