下面的代码适用于 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