2015-05-06 74 views
1

請參閱更新在問題結束時爲解決方案感謝標記答案!有沒有辦法在同一個查詢中重用子查詢?

我想對待一個子查詢,就好像它是一個可以在相同查詢中重用的實際表一樣。下面是安裝SQL:

create table mydb.mytable 
(
    id integer not null, 
    fieldvalue varchar(100), 
    ts timestamp(6) not null 
) 
unique primary index (id, ts) 

insert into mydb.mytable(0,'hello',current_timestamp - interval '1' minute); 
insert into mydb.mytable(0,'hello',current_timestamp - interval '2' minute); 
insert into mydb.mytable(0,'hello there',current_timestamp - interval '3' minute); 
insert into mydb.mytable(0,'hello there, sir',current_timestamp - interval '4' minute); 
insert into mydb.mytable(0,'hello there, sir',current_timestamp - interval '5' minute); 
insert into mydb.mytable(0,'hello there, sir. how are you?',current_timestamp - interval '6' minute); 

insert into mydb.mytable(1,'what up',current_timestamp - interval '1' minute); 
insert into mydb.mytable(1,'what up',current_timestamp - interval '2' minute); 
insert into mydb.mytable(1,'what up, mr man?',current_timestamp - interval '3' minute); 
insert into mydb.mytable(1,'what up, duder?',current_timestamp - interval '4' minute); 
insert into mydb.mytable(1,'what up, duder?',current_timestamp - interval '5' minute); 
insert into mydb.mytable(1,'what up, duder?',current_timestamp - interval '6' minute); 

我想要做的是隻返回行,其中FieldValue從以前的行不同。這個SQL做到了這一點:

locking row for access 
select id, fieldvalue, ts from 
(
    --locking row for access 
    select 
     id, fieldvalue, 
     min(fieldvalue) over 
     (
      partition by id 
      order by ts, fieldvalue rows 
      between 1 preceding and 1 preceding 
     ) fieldvalue2, 
     ts 
    from mydb.mytable 
) x 
where 
    hashrow(fieldvalue) <> hashrow(fieldvalue2) 
order by id, ts desc 

它返回:

+----+---------------------------------+----------------------------+ 
| id |   fieldvalue   |    ts    | 
+----+---------------------------------+----------------------------+ 
| 0 | hello       | 2015-05-06 10:13:34.160000 | 
| 0 | hello there      | 2015-05-06 10:12:34.350000 | 
| 0 | hello there, sir    | 2015-05-06 10:10:34.750000 | 
| 0 | hello there, sir. how are you? | 2015-05-06 10:09:34.970000 | 
| 1 | what up       | 2015-05-06 10:13:35.470000 | 
| 1 | what up, mr man?    | 2015-05-06 10:12:35.690000 | 
| 1 | what up, duder?     | 2015-05-06 10:09:36.240000 | 
+----+---------------------------------+----------------------------+

下一步是每個ID只返回了最後一排。如果我用這個SQL寫的前一個選擇到表...

create table mydb.reusetest as (above sql) with data; 

......然後我可以做到這一點做得到每個ID的最後一行:

locking row for access 
select t1.* from mydb.reusetest t1, 
(
    select id, max(ts) ts from mydb.reusetest 
    group by id 
) t2 
where 
    t2.id = t1.id and 
    t2.ts = t1.ts 
order by t1.id 

會返回這個:

+----+------------+----------------------------+ 
| id | fieldvalue |    ts    | 
+----+------------+----------------------------+ 
| 0 | hello  | 2015-05-06 10:13:34.160000 | 
| 1 | what up | 2015-05-06 10:13:35.470000 | 
+----+------------+----------------------------+

如果我可以在我的初始SELECT中重用子查詢,我可以實現相同的結果。我可以將整個查詢SQL複製/粘貼到另一個子查詢中以創建派生表,但這意味着如果需要修改它,我需要在兩處更改SQL。

更新

感謝克里斯蒂安,我是能夠實現WITH子句到我的SQL像這樣完美的結果:

locking row for access 
with items (id, fieldvalue, ts) as 
(
    select id, fieldvalue, ts from 
    (
     select 
      id, fieldvalue, 
      min(fieldvalue) over 
      (
       partition by id 
       order by ts, fieldvalue 
       rows between 1 preceding and 1 preceding 
      ) fieldvalue2, 
      ts 
     from mydb.mytable 
    ) x 
    where 
     hashrow(fieldvalue) <> hashrow(fieldvalue2) 
) 
select t1.* from items t1, 
(
    select id, max(ts) ts from items 
    group by id 
) t2 
where 
    t2.id = t1.id and 
    t2.ts = t1.ts 
order by t1.id 

回答

1

是否WITH幫助?這可讓您定義可在SELECT中多次使用的結果集。

從他們的例子:

WITH orderable_items (product_id, quantity) AS 
(SELECT stocked.product_id, stocked.quantity 
    FROM stocked, product 
    WHERE stocked.product_id = product.product_id 
    AND product.on_hand > 5 
) 

SELECT product_id, quantity 
FROM orderable_items 
WHERE quantity < 10; 
+0

這完美的作品。在我的答案結尾處查看解決方案。謝謝! – oscilatingcretin

相關問題