我在工作中使用 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 是否有这种可能性?