2017-05-29 110 views
0

我想在外部混合每個位置的時區信息過濾具有時間戳的行不帶時區值。Jooq notation with multiple arrays

我怎樣才能在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) 
+0

哦,使用'WITH ORDINALITY'非常好的解決方案。也許這個問題應該得到更高的優先權。 –

回答

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")); 
+1

您仍然可以使用'DSL.unnest()',然後將結果換成普通SQL,例如:'DSL.table(「{0} WITH ORDINALITY」,DSL.unnest(...))' –

+0

建議的改進替代品'array_table'字符串,而不是數組參數。 –