SQL查询,以获取下一行的列为行的列,列的第二行为下一行

时间:2017-06-16 22:18:32

标签: sql sql-server

我想编写一个SQL查询,以使下一行的列成为一行的列。测试示例如下

enter image description here

我想获得SQL查询以获得如下结果:

enter image description here

1 个答案:

答案 0 :(得分:1)

如果您所使用的版本支持LEAD,则可以使用select customer,orderdate,item, lead(orderdate,1) over(partition by customer order by date) as second_date, lead(item,1) over(partition by customer order by date) as second_item, lead(orderdate,2) over(partition by customer order by date) as third_date, lead(item,2) over(partition by customer order by date) as third_item from tbl 执行此操作。

enter code here

<tr class="clickable-row">
<td colspan="3"><strong><a href="/mathdept.ucr.edu">CENTER FOR MATHEMATICAL &amp; COMPUTATIONAL MODELING IN BIOLOGY &amp; MEDICINE<span style="float: right;">►►</span></a><br /></strong></td>
</tr>