假设我有这个表:
select * from window_test;
k | v
---+---
a | 1
a | 2
b | 3
a | 4
Run Code Online (Sandbox Code Playgroud)
最终我想得到:
k | min_v | max_v
---+-------+-------
a | 1 | 2
b | 3 | 3
a | 4 | 4
Run Code Online (Sandbox Code Playgroud)
但我会很高兴得到这个(因为我可以很容易地过滤它distinct):
k | min_v | max_v
---+-------+-------
a | 1 | 2
a | 1 | 2
b | 3 | 3
a | 4 | 4
Run Code Online (Sandbox Code Playgroud)
使用PostgreSQL 9.1+窗口函数可以实现这一点吗?我试图了解我是否可以使用单独的分区来处理k=a此示例中的第一次和最后一次(按顺序排列v).
我有一个查询,使用FULL JOIN需要2.5秒,使用INNER,RIGHT或LEFT JOIN需要40秒.
这是查询.子查询(完成两次)只需要1.3秒.
SELECT T1.[time], T1.Total, T1.rn, T2.[time], T2.Total, T2.rn
FROM
(
select [time], MAX(ComputedValue) as Total, row_number() over (order by [time]) as rn
FROM
(
select SUBSTRING(CONVERT(CHAR(10), IntervalStartTime, 108), 0, 6) as [time], ComputedValue
from LoadTestTransactionSample
where LoadTestRunId=285
and CounterName='Total Transactions'
and TransactionName='Export'
) foo
group by [time]
) T1
_____ JOIN
(
select [time], MAX(ComputedValue) as Total, row_number() over (order by [time]) as rn
FROM
(
select SUBSTRING(CONVERT(CHAR(10), IntervalStartTime, 108), 0, 6) as [time], ComputedValue
from LoadTestTransactionSample …Run Code Online (Sandbox Code Playgroud)