获取序列中的最后一个值
Get last value in sequence
我正在尝试 return 特定事件序列中的最后一个值,我最初的想法是使用 LAST_VALUE() 但我无法让它工作。我可以用子查询和连接来做到这一点,但是是否有一个 window 函数可以更容易地给出这个结果?
现在查询正在提取最大数量,但我想要的是基于 seq 列的 last 数量
数据
| PaymentID | Description | Result | Seq |
|-----------|----------------------|--------|-----|
| 1 | Entered Payment Page | Yes | 1 |
| 1 | Amount Entered | 50 | 2 |
| 1 | Amount Entered | 60 | 3 |
| 1 | Amount Entered | 20 | 4 |
| 1 | Amount Confirmed | Yes | 5 |
| 2 | Entered Payment Page | Yes | 1 |
| 2 | Amount Entered | 100 | 2 |
| 2 | Amount Confirmed | Yes | 3 |
| 3 | Entered Payment Page | Yes | 1 |
| 3 | Amount Entered | 4 | 2 |
| 3 | Amount Confirmed | No | 3 |
| 3 | Amount Entered | 8 | 4 |
| 3 | Amount Confirmed | Yes | 5 |
当前查询结果
| PaymentID | InPayment | Amount | Confirmed |
|-----------|-----------|--------|-----------|
| 1 | Yes | 60 | Yes |
| 2 | Yes | 100 | Yes |
| 3 | Yes | 8 | Yes |
想要的结果
| PaymentID | InPayment | Amount | Confirmed |
|-----------|-----------|--------|-----------|
| 1 | Yes | 20 | Yes |
| 2 | Yes | 100 | Yes |
| 3 | Yes | 8 | Yes |
您可以使用 row_number()
和条件聚合:
select paymentid,
max(case when description = 'Entered Payment Page' then result end) as inpayment,
max(case when description = 'Amount Entered' then result end) as amount_entered,
max(case when description = 'Amount Confirmed' then result end) as amount_confirmed
from (select t.*,
row_number() over (partition by paymentid, description order by seq desc) as seqnum
from paymentinfo t
) t
where seqnum = 1
group by paymentid;
Here 是 SQL Fiddle.
我正在尝试 return 特定事件序列中的最后一个值,我最初的想法是使用 LAST_VALUE() 但我无法让它工作。我可以用子查询和连接来做到这一点,但是是否有一个 window 函数可以更容易地给出这个结果?
现在查询正在提取最大数量,但我想要的是基于 seq 列的 last 数量
数据
| PaymentID | Description | Result | Seq |
|-----------|----------------------|--------|-----|
| 1 | Entered Payment Page | Yes | 1 |
| 1 | Amount Entered | 50 | 2 |
| 1 | Amount Entered | 60 | 3 |
| 1 | Amount Entered | 20 | 4 |
| 1 | Amount Confirmed | Yes | 5 |
| 2 | Entered Payment Page | Yes | 1 |
| 2 | Amount Entered | 100 | 2 |
| 2 | Amount Confirmed | Yes | 3 |
| 3 | Entered Payment Page | Yes | 1 |
| 3 | Amount Entered | 4 | 2 |
| 3 | Amount Confirmed | No | 3 |
| 3 | Amount Entered | 8 | 4 |
| 3 | Amount Confirmed | Yes | 5 |
当前查询结果
| PaymentID | InPayment | Amount | Confirmed |
|-----------|-----------|--------|-----------|
| 1 | Yes | 60 | Yes |
| 2 | Yes | 100 | Yes |
| 3 | Yes | 8 | Yes |
想要的结果
| PaymentID | InPayment | Amount | Confirmed |
|-----------|-----------|--------|-----------|
| 1 | Yes | 20 | Yes |
| 2 | Yes | 100 | Yes |
| 3 | Yes | 8 | Yes |
您可以使用 row_number()
和条件聚合:
select paymentid,
max(case when description = 'Entered Payment Page' then result end) as inpayment,
max(case when description = 'Amount Entered' then result end) as amount_entered,
max(case when description = 'Amount Confirmed' then result end) as amount_confirmed
from (select t.*,
row_number() over (partition by paymentid, description order by seq desc) as seqnum
from paymentinfo t
) t
where seqnum = 1
group by paymentid;
Here 是 SQL Fiddle.