如何在SQL Server中将两行合并为一行 [英] How to Merge two Rows into one in sql Server

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

问题描述

我想将两行合并成一行可以任何一个帮助

I would like to merge two rows into single row can any one help

推荐答案

嗨...



请参阅以下示例。



Hi ...

Please see the below example.

CREATE TABLE #Temp
(
[Numbers] varchar(40)
)
INSERT INTO #Temp VALUES('One');
INSERT INTO #Temp VALUES('Two');
INSERT INTO #Temp VALUES('Three');
INSERT INTO #Temp VALUES('Four');
INSERT INTO #Temp VALUES('Five');

select * from #Temp

DECLARE @str VARCHAR(100)
SELECT @str = COALESCE(@str + '', '') + [Numbers]
FROM #Temp
Print @str






从table_name中选择col_name1 +''''+ col_name2
Hi,

select col_name1 + '''' + col_name2 from table_name


use Join

use Join
select tbl1.fld1 as t1_fld1,tbl1.fld2 as t1_fld2
,tbl2.fld1 as t2_fld1 ,tbl2.fld2 as t2_fld2
from
tbl1 
inner join tbl2 on tbl1.fld1=tbl2.fld1




tbl1
-------------
fld1   fld2
-------------
a      b
1      2  


tbl2
-------------
fld1   fld2
-------------
a      c
1      3


Result
---------------------------------------
t1_fld1   t1_fld2   t2_fld1   t2_fld2
---------------------------------------
a         b         a          c
1         2         1          3


two different tables rows are merged into single row using join



这种方式首先和第二个表的相关行将在结果表集中显示为

单行。



如果您通过示例提出问题,然后它会更容易理解。



快乐编码!

:)


this way first & second tables'' related rows will be shown as
single row in resultant table-set.

if you ask your question with example, then it will be more easy to understand.

Happy Coding!
:)


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

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