加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > MsSql教程 > 正文

SQLServer 将两行数据合并到一行

发布时间:2020-12-12 14:05:20 所属栏目:MsSql教程 来源:网络整理
导读:一、问题描述 存储一个环结点的表,输出相邻环结点记录。 环结点表如下: ? ? ? ? ? ? ? ? ? ? ? ? ? 预期结果: 1-3,3-4,4-1. ? ? ? ? ? ? ? ?? ? ?? 二、解决方案 declare @minVertex intSelect @minVertex= min(Vertex) from CircleVertexselect vertex a

一、问题描述

存储一个环结点的表,输出相邻环结点记录。

环结点表如下:

? ? ? ? ? ? ? ? ? ? ? ? ?


预期结果:1->3,3->4,4->1.

? ? ? ? ? ? ? ??

? ??


二、解决方案

declare @minVertex int
Select @minVertex= min(Vertex) from CircleVertex

select vertex as parentVertex,isnull((select top 1 vertex from circleVertex R
	 where R.id > T.id
	 order by R.id
    ),@minVertex) as childVertex
from circleVertex T

三、参考

[1].SQL Server combining 2 rows into 1 from the same table

[2].How to combine 2 rows into 1 from the same table



期待您更好的解决方案!

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读