如何使用tsql从单行进出 [英] How do I get from and to from a single row using tsql
本文介绍了如何使用tsql从单行进出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有以下数据
间隔
1
2
3
4
5
想要使用tsql实现以下功能
从
0 1
1 2
2 3
3 4
4 5
我尝试过:
我尝试过使用ROW_NUMBER()
选择ROW_NUMBER()超过(按间隔排序)row_id,从mytable开始间隔
但是无法弄清楚方法。
I have the following data
interval
1
2
3
4
5
and want to achieve the following using tsql
from to
0 1
1 2
2 3
3 4
4 5
What I have tried:
I have tried using ROW_NUMBER()
select ROW_NUMBER() over (order by interval) row_id,interval from mytable
but can't figure out a way.
推荐答案
假设您使用的是SQL 2012或更高版本,请尝试使用 LAG [ ^ ]:
Assuming you're using SQL 2012 or later, try using LAG[^]:
SELECT
IsNull(LAG(interval) OVER (ORDER BY interval), 0) As [from],
interval As [to]
FROM
YourTable
ORDER BY
interval
;
这篇关于如何使用tsql从单行进出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文