SQL语句怎么实现几列数据合并成一条??

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了SQL语句怎么实现几列数据合并成一条??相关的知识,希望对你有一定的参考价值。

表1:
sCode 编号字段

表2:
sCode 表1的sCode,关联用
sQuality 品质
fNum 数量

表1:
sCode
001
002
003

表2:
sCode,sQuality,fNum
001 a 10
001 b 15
001 c 20
002 a 10
002 b 90
002 c 80

最后结果为:
001,a,10,b,15,c,20
002,a,10,b,90,c,80

执行结果就要把三条001和三条002的数据各合并成一条,请高手指教!!!
表结构应该没问题,只要能实现出最后的结果就可以,用的数据库是sqlserver,请高手指导,谢谢!!!

参考技术A select DISTINCT ta.sCode, ta.sQuality, ta.fNum, tb.sQuality, tb.fNum, tc.sQuality, tc.fNum
from
(

select sCode, sQuality, max(fNum) as fNum from 表2
where sQuality = 'a'
group by sCode, sQuality

) as ta left join (

select sCode, sQuality, max(fNum) as fNum from 表2
where sQuality = 'b'
group by sCode, sQuality

) as tb on tb.sCode = ta.sCode left join (

select sCode, sQuality, max(fNum) as fNum from 表2
where sQuality = 'c'
group by sCode, sQuality

) as tc on tc.sCode = ta.sCode
参考技术B 是什么数据库oracle ?,sqlserver?
oralce :
CREATE OR REPLACE FUNCTION get_Val(tmp_c1 NUMBER) RETURN VARCHAR2 IS
col_temp VARCHAR2(4000);
BEGIN
FOR cur IN (SELECT squality, fnum FROM t_02 WHERE scode = tmp_c1) LOOP
col_temp := col_temp || ' ' || cur.squality || ' ' || cur.fnum;
END LOOP;
col_temp := rtrim(col_temp, 1);
RETURN col_temp;
END;
/

select distinct a.sCode, get_Val(b.scode) "RESULT"
from t_01 a
join t_02 b on a.scode = b.scode;
参考技术C 建议楼主修改表的结构,现在这个结构似乎实现不了。表一放sCode和sQuality,表2放sCode和fNum 参考技术D 期待高手`~ 第5个回答  2008-08-31 要用sqlserver数据库的语句,fjh658 谢谢你的回答,但是运行后显示的错误
“ 服务器: 消息 195,级别 15,状态 10,行 1
'get_Val' 不是可以识别的 函数名。 ”

一、以下是一种形式,但是并不是我最想要的结果,大家可以借鉴下,有好的提议可以尽量提出来.

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[表1]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)
drop table [dbo].[表1]
GO

CREATE TABLE [dbo].[表1] (
[sCode] [varchar] (50) COLLATE Chinese_PRC_CI_AS NULL
) ON [PRIMARY]
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[表2]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)
drop table [dbo].[表2]
GO

CREATE TABLE [dbo].[表2] (
[sCode] [varchar] (50) COLLATE Chinese_PRC_CI_AS NULL ,
[sQuality] [varchar] (50) COLLATE Chinese_PRC_CI_AS NULL ,
[fNum] [int] NULL
) ON [PRIMARY]
GO

CREATE FUNCTION dbo.f_str1(@sCode varchar(10))
RETURNS varchar(100)
AS
BEGIN
DECLARE @re varchar(100)
SET @re=''
SELECT @re=@re+','+CAST(squality as varchar)+','+CAST(fNum as varchar)
FROM 表2
WHERE sCode=@sCode
RETURN(STUFF(@re,1,1,''))
END
GO

SELECT sCode,sQuality=dbo.f_str1(sCode) FROM 表2 GROUP BY sCode
把里面的数据都在查询分析器里运行一次就可以得到我想要的结果了,只是这样的结果只有两个字段,我想要的是每个数据都一个列字段,虽然这个效果不好,但是有兴趣的人,可以试试看

二、我认为较为精确的算法
declare @sqlstr varchar (2000),@sQuality varchar (50),@iLoop int
select @sqlstr=''
select @iLoop=1
Declare ItemList
Cursor for select sQuality from table2 group by sQuality
Open ItemList
Fetch next from ItemList into @sQuality
while @@Fetch_STATUS=0
BEGIN
select @sqlstr=isnull(@sqlstr,'')+ ',(select top 1 sQuality from table2 where a.scode=scode and sQuality='''+isnull(@sQuality,'')+''' group by sQuality) Item'+cast(@iLoop as varchar) +
',(select fnum from table2 where a.scode=scode and sQuality='''+isnull(@sQuality,'')+''' group by fnum) ItemNum'+cast(@iLoop as varchar)
select @iLoop=@iLoop+1
Fetch next from ItemList into @sQuality
END
close ItemList
DEALLOCATE ItemList
select @sqlstr= 'select scode' +isnull(@sqlstr,'') + ' from table2 a where scode in (select scode from table1) group by scode'
Exec (@sqlstr)

署名: 都是问题惹的祸本回答被提问者采纳

SQL 多行多列数据清洗合并为一行

参考技术A SELECT  

            A,

            GROUP_CONCAT( B  SEPARATOR  ','  ),

            GROUP_CONCAT( C  SEPARATOR   ','  )

FROM

            table_name 

GROUP BY

            A

1.GROUP_CONCAT()中的值为你要合并的数据的字段名;

2.SEPARATOR 函数是用来分隔这些要合并的数据的 ,' ' 中是你要用哪个符号来分隔,可以直接不填符号默认为空值;

2.必须要用GROUP BY 语句来进行分组管理,不然所有的数据都会被合并成一条记录

参考链接

https://www.cnblogs.com/shoshana-kong/p/11147690.html

以上是关于SQL语句怎么实现几列数据合并成一条??的主要内容,如果未能解决你的问题,请参考以下文章

SQL怎么列合并

mysql sql语句两条合并成一条

求助:SQLSERVER多条查询语句合并

如何用sql语句将多行合并成一行

求助,oracle多行数据合并成一行

求助,oracle多行数据合并成一行