如何在SQL Server中将多行更改为单行 [英] how to change multiple row as a single row in sql server

查看:94
本文介绍了如何在SQL Server中将多行更改为单行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在sql server中将多行转换为单行(多列)。



例如,



sql数据后跟,



 name年龄

Anbu 45
senthil 25
Raj 27







我所需的输出是



 Anbu  45  Senthil  25  Raj  27  







怎么做?

解决方案

我很少需要将列数据转换为行数据 - 通常是以标记方式存储数据时。这是通过同一个表上的内连接来实现的,但每个上都有不同的别名和约束。



  SELECT  A.name,A,age,B.name,B.age,C.name,C.age 
FROM YOUR_TABLE A
INNER 加入 YOUR_TABLE as B
ON A.something = b.something
INNER JOIN YOUR_TABLE C
ON B.something = c.something
WHERE A.whatever = value
AND B.whatever = value
AND C.whatever = value



等等等等



您需要确定如何链接这些。

I have convert multiple row into single row(multiple column) in sql server.

For example,

sql data is followed by,

name     Age

Anbu     45
senthil  25
Raj      27




My required output is

Anbu  45  Senthil  25  Raj 27




How to do this?

解决方案

I not infrequently need to convert column data into row data - typically when data is stored in tagged fashion. This is accomplished by inner-joins on the same table, but with different alias' and constraints on each one.

SELECT A.name, A,age, B.name, B.age, C.name, C.age
 FROM YOUR_TABLE as A
  INNER JOIN YOUR_TABLE as B
    ON A.something=b.something 
  INNER JOIN YOUR_TABLE C
    ON B.something=c.something 
 WHERE A.whatever=value 
 AND B.whatever=value 
 AND C.whatever=value


etc. etc. etc.

You'll need to determine how to link these.


这篇关于如何在SQL Server中将多行更改为单行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆