Teradata 有没有办法在同一查询中重用子查询?

Teradata 有没有办法在同一查询中重用子查询?,teradata,Teradata,请参阅问题末尾的更新,以获取解决方案,感谢您的标记答案 我希望将子查询视为可以在同一查询中重用的实际表。以下是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_

请参阅问题末尾的更新,以获取解决方案,感谢您的标记答案

我希望将子查询视为可以在同一查询中重用的实际表。以下是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的最后一行:

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,我需要在两个地方对其进行更改

更新

感谢Kristján,我能够像这样在SQL中实现WITH子句以获得完美的结果:

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
有帮助吗?它允许您定义一个结果集,您可以在“选择”对话框中多次使用该结果集

从他们的例子来看:

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;

这很好用。请参阅我答案末尾的解决方案。谢谢
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
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;