www.qcfz.net > sql语句查出多行数据,如何将ID相同的行并成一行,...

sql语句查出多行数据,如何将ID相同的行并成一行,...

相同ID合并成一行,只需要用联合查询就可以了 不相同的字段合成一个字符串只需要连接符+然后 AS一个别名就OK了~

select distinct * from( SELECT 编号, 浏览 = ( SELECT 浏览+'' FROM 表名 AS b WHERE b.编号 = a.编号 FOR XML PATH('') ), 搜索 = ( SELECT 搜索+'' FROM 表名 AS c WHERE c.编码 = a.编码 FOR XML PATH('') ), 购买 FROM [test].[dbo].[Shee...

这个恐怕要用存贮过程或编程实现, 提个思路: 1) 创建一个同字段结构的新表table2 2) 按Col1排序 3) 循环每条记录, 记录每个字段值到临时变量中, 名为vCol1, vCol2... if (Col1 == 前一条记录vCol1) 累加vCol2, vCol3...(如果是字符串则相连) els...

--sql2000Create Function fn_hb(@id Varchar(100))Returns Varchar(8000)AsBegin Declare @Rst Varchar(8000) Set @Rst='' Select @Rst=@Rst+name+',' From test Where id=@id Set @Rst=Left(@Rst,len(@RSt)-1) Return @RstEndgo--调用select i...

要达到你说的结果, 单靠sql语句难度很大(你的这个相同id的数据行数不确定吧?). 给你个思路供参考: 先循环读出并合并相同id的数据, 然后再写入一新表中.

SELECT * FROM (SELECT T.ID, LEAD(T.NAME, 0, '') OVER(PARTITION BY T.ID ORDER BY T.ID), LEAD(T.NAME, 1, '') OVER(PARTITION BY T.ID ORDER BY T.ID), LEAD(T.NAME, 2, '') OVER(PARTITION BY T.ID ORDER BY T.ID), ROW_NUMBER() OVER(PART...

select id,sum(isnull(data1,0)) data1,sum(isnull(data2,0)) data2 from 表1 group by idsqlserver写法,其他数据库略有不同

select id, max(case when rn = 1 then op end ) op_1, max(case wehn rn = 1 then result end ) result_1, max(case when rn = 1 then else end ) else_1, max(case when rn = 2 then op end ) op_2, max(case wehn rn = 2 then result end ) r...

这个比较复杂:( WITH cte AS ( --找出分组的最小行号 SELECT * ,Min(ID) OVER (PARTITION BY Code, 数值) AS MinID --添加1列,使用开窗函数 FROM TableX)SELECT ID, Code, 数值 ,X.数值 AS [D]FROM cte--然后关联原表LEFT JOIN TableX X ON c...

DECLARE @combinedString VARCHAR(MAX) SELECT @combinedString = COALESCE(@combinedString + ', ', '') + DATA FROM TABLENAME WHERE ID = 1 select @combinedString

网站地图

All rights reserved Powered by www.qcfz.net

copyright ©right 2010-2021。
www.qcfz.net内容来自网络,如有侵犯请联系客服。zhit325@qq.com