【发布时间】:2013-05-31 20:55:24
【问题描述】:
我在工作中使用 SQL Server,并且我有一些 OUTER APPLY 子句的好技巧,可以帮助我不重复代码。例如,如果我有一个这样的表:
create table Transactions
(
ID bigint identity(1, 1) primary key, [Date] datetime, Amount decimal(29, 2), Amount2 decimal(29, 2)
)
insert into Transactions ([Date], Amount, Amount2)
select getdate(), 100.00, null union all
select getdate(), 25.00, 75.00
我想从中选择数据,例如我将为每个不为空的金额设置一行,我可以这样查询:
select
T.ID,
T.[Date],
OA.Amount
from Transactions as T
outer apply (
select T.Amount as Amount union all
select T.Amount2 as Amount
) as OA
where OA.Amount is not null
而不是使用union:
select
T.ID,
T.[Date],
T.Amount
from Transactions as T
where T.Amount is not null
union all
select
T.ID,
T.[Date],
T.Amount2 as Amount
from Transactions as T
where T.Amount2 is not null
所以我想知道 - 其他 RDBMS 是否有这种可能性?
【问题讨论】:
-
PostgreSQL 9.3 将具有
LATERAL,这是 SQL Server 的outer apply的 ANSI 等效项 -
谢谢,所以有 ANSI 等价物:)
-
手册中有一些例子:postgresql.org/docs/9.3/static/…
标签: mysql sql sql-server oracle postgresql