0

下面的代码适用于 MSSQL。关于如何将其翻译为 Postgre 的任何建议?

;with mySource  as (
  SELECT 1050
LineID, 1 SeqNo, NULL Val
UNION SELECT 1050 LineID, 2
SeqNo, NULL Val
UNION SELECT 1050 LineID, 3
SeqNo, 'ABC' Val
UNION SELECT 1050 LineID, 4
SeqNo, NULL Val
UNION SELECT 1050 LineID, 5
SeqNo, NULL Val
UNION SELECT 1050 LineID, 6
SeqNo, 'CDE' Val
UNION SELECT 1050 LineID, 7
SeqNo, NULL Val
UNION SELECT 1050 LineID, 8
SeqNo, NULL Val
UNION SELECT 1050 LineID, 9
SeqNo, 'EFG' Val
UNION SELECT 1050 LineID, 10
SeqNo, NULL Val
UNION SELECT 2222 LineID, 1
SeqNo, NULL Val
UNION SELECT 2222 LineID, 2
SeqNo, 'ABC' Val
UNION SELECT 2222 LineID, 3
SeqNo, 'CDE' Val
UNION SELECT 2222 LineID, 4
SeqNo, NULL Val
UNION SELECT 2222 LineID, 5
SeqNo, NULL Val
UNION SELECT 2222 LineID, 6
SeqNo, 'EFG' Val
UNION SELECT 2222 LineID, 7
SeqNo, NULL Val
UNION SELECT 2222 LineID, 8
SeqNo, 'HIJ' Val
UNION SELECT 2222 LineID, 9
SeqNo, NULL Val
UNION SELECT 2222 LineID, 10
SeqNo, 'KLM' Val
) 
Select LineID,SeqNo, Coalesce(bu,ba) Val 
from mySource m
outer apply (select top 1 Val 
from mySource m1 
WHERE m1.LineID=m.LineID and m1.SeqNo<=m.SeqNo and Val is not null 
Order by SeqNo DESC) d1(bu) 
outer APPLY (SELECT TOP 1 Val 
FROM mySource m3 
WHERE  m3.LineID=m.LineID and m3.SeqNo>= m.SeqNo AND Val IS NOT NULL 
ORDER  BY SeqNo) d3(ba)
ORDER BY m.LineID, m.SeqNo
4

1 回答 1

1

outer applyPosgres 中的等价物是left join lateral. 您还需要将TOP 1特定于 T-SQL 的 替换为LIMIT.

也可以缩短公用表表达式以使用该values()语法。

with mySource(LineID, SeqNo, Val)  as (values
    (1050, 1, null),
    (1050, 2, null),
    (1050, 3, null),
    ...
    (2222, 10, 'KLM')
)
select LineID, SeqNo, Coalesce(bu,ba) Val 
from mySource m
left join lateral (
    select Val bu 
    from mySource m1 
    where m1.LineID = m.LineID and m1.SeqNo <= m.SeqNo and Val is not null 
    order by SeqNo desc
    limit 1
) d1 on true 
left join lateral (
    select Val ba
    from mySource m3 
    where m3.LineID = m.LineID and m3.SeqNo >= m.SeqNo AND Val is not null 
    order  by SeqNo
    limit 1
) d3 on true
order by m.LineID, m.SeqNo

查看查询,我倾向于怀疑它的逻辑可以通过窗口函数在很大程度上简化(lag()lead()想到)。您可能想问另一个问题,其中包含有关您要完成的工作的更多详细信息,以及示例数据和预期结果。

于 2020-03-30T21:25:26.010 回答