多行合并为一行的SQL语句
目录
例一 1
例二 2
例三 3
例四 5
例五 6
例六 如何将多行数据合并成一行多列 7
例七 C# 12
✧ 例一
表数据:test
no q
------------
1 n1
1 n2
1 n3
1 n4
1 n5
3 t1
3 t2
3 t3
3 t4
3 t5
3 t6
1 n2
1 n3
1 n4
1 n5
3 t1
3 t2
3 t3
3 t4
3 t5
3 t6
2 m1
语句:
with test as (
SELECT 1 AS No, 'N1' AS q FROM Dual UNION ALL
SELECT 1 AS No, 'N2' AS q FROM Dual UNION ALL
SELECT 1 AS No, 'N3' AS q FROM Dual UNION ALL
SELECT 1 AS No, 'N4' AS q FROM Dual UNION ALL
SELECT 1 AS No, 'N5' AS q FROM Dual UNION ALL
SELECT 3 AS No, 'T1' AS q FROM Dual UNION ALL
SELECT 3 AS No, 'T2' AS q FROM Dual UNION ALL
SELECT 3 AS No, 'T3' AS q FROM Dual UNION ALL
SELECT 3 AS No, 'T4' AS q FROM Dual UNION ALL
SELECT 3 AS No, 'T5' AS q FROM Dual UNION ALL
SELECT 3 AS No, 'T6' AS q FROM Dual UNION ALL
SELECT 1 AS No, 'N1' AS q FROM Dual UNION ALL
SELECT 1 AS No, 'N2' AS q FROM Dual UNION ALL
SELECT 1 AS No, 'N3' AS q FROM Dual UNION ALL
SELECT 1 AS No, 'N4' AS q FROM Dual UNION ALL
SELECT 1 AS No, 'N5' AS q FROM Dual UNION ALL
SELECT 3 AS No, 'T1' AS q FROM Dual UNION ALL
SELECT 3 AS No, 'T2' AS q FROM Dual UNION ALL
SELECT 3 AS No, 'T3' AS q FROM Dual UNION ALL
SELECT 3 AS No, 'T4' AS q FROM Dual UNION ALL
SELECT 3 AS No, 'T5' AS q FROM Dual UNION ALL
SELECT 3 AS No, 'T6' AS q FROM Dual UNION ALL
SELECT 2 AS No, 'M1' AS q FROM Dual
)
SELECT No, Substr(Jg, 2, Length(Jg)) AS Jgsql中union多表合并
FROM (SELECT No, MAX(Sys_Connect_By_Path(q, ',')) AS Jg
FROM (SELECT No,
q,
Row_Number() Over(PARTITION BY No ORDER BY No, q) Rn
FROM Test)
START WITH Rn = 1
CONNECT BY Rn - 1 = PRIOR Rn
AND No = No
GROUP BY No)
)
SELECT No, Substr(Jg, 2, Length(Jg)) AS Jgsql中union多表合并
FROM (SELECT No, MAX(Sys_Connect_By_Path(q, ',')) AS Jg
FROM (SELECT No,
q,
Row_Number() Over(PARTITION BY No ORDER BY No, q) Rn
FROM Test)
START WITH Rn = 1
CONNECT BY Rn - 1 = PRIOR Rn
AND No = No
GROUP BY No)
谭工前面那个示例"CONNECT BY Rn - 1 = PRIOR Rn AND No = No"也应改成"CONNECT BY No||'|'||(Rn - 1) = PRIOR (NO||'|'||Rn)"结果才对。
另外这个SQL只适合在小数据集或索引结果中进行处理,不然要使用全表扫描效率不高。
输出:
1 ,N1,N2,N3,N4,N5
2 ,M1
3 ,T1,T2,T3,T4,T5,T6
2 ,M1
3 ,T1,T2,T3,T4,T5,T6
✧ 例二
另解:
人员所属部门:第一个部门为缺省部门
SELECT 人员编号,
MIN(姓名) AS 姓名,
Substr(MAX(To_Char(Length(部门), '99999999') || 部门), 11, 2000) AS 部门
FROM (SELECT 人员编号, 姓名, Sys_Connect_By_Path(部门, ',') AS 部门
FROM (SELECT 姓名,
部门,
SELECT 人员编号,
MIN(姓名) AS 姓名,
Substr(MAX(To_Char(Length(部门), '99999999') || 部门), 11, 2000) AS 部门
FROM (SELECT 人员编号, 姓名, Sys_Connect_By_Path(部门, ',') AS 部门
FROM (SELECT 姓名,
部门,
缺省,
人员编号,
Row_Number() Over(PARTITION BY 姓名 ORDER BY 人员编号, 缺省 DESC) Rn
FROM (SELECT c.姓名,
b.名称 AS 部门,
a.缺省,
c.编号 AS 人员编号,
b.编码 AS 部门编码
FROM 部门人员 a, 部门表 b, 人员表 c
WHERE a.部门id = b.Id
AND a.人员id = c.Id
) t)
START WITH Rn = 1
CONNECT BY (人员编号 || (Rn - 1)) = PRIOR (人员编号 || Rn))
人员编号,
Row_Number() Over(PARTITION BY 姓名 ORDER BY 人员编号, 缺省 DESC) Rn
FROM (SELECT c.姓名,
b.名称 AS 部门,
a.缺省,
c.编号 AS 人员编号,
b.编码 AS 部门编码
FROM 部门人员 a, 部门表 b, 人员表 c
WHERE a.部门id = b.Id
AND a.人员id = c.Id
) t)
START WITH Rn = 1
CONNECT BY (人员编号 || (Rn - 1)) = PRIOR (人员编号 || Rn))
GROUP BY 人员编号
ORDER BY 部门
ORDER BY 部门
CONNECT BY (人员编号 || (Rn - 1)) = PRIOR (人员编号 || Rn)
这一句整很久才整出来,为了避免不等长的编号,下面的改进也许更合适:
CONNECT BY (人员编号 || '|' || (Rn - 1)) = PRIOR (人员编号 || '|' || Rn)
这样就不会出现错乱了.
✧ 例三
如何将一个表中的多行数据合并为一行新数据?
比如有表A
ID Field1 Field2 Field3
1 b 10.1 12.2
2 b 9.8 8.6
ID Field1 Field2 Field3
1 b 10.1 12.2
2 b 9.8 8.6
3 c 12.3 11.2
4 c 5.3 8.2
现在想生成下面这样的数据
ID Field1 Field2 Field3
1 b 10.1 12.2
2 b 9.8 8.6
3 c 12.3 11.2
4 c 5.3 8.2
5 b_all 19.9 20.8 注:19.9=10.1+9.8 20.8=12.2+8.6
6 c_all 17.6 19.4
请问如何用SQL语句或存贮过程来实现?
4 c 5.3 8.2
现在想生成下面这样的数据
ID Field1 Field2 Field3
1 b 10.1 12.2
2 b 9.8 8.6
3 c 12.3 11.2
4 c 5.3 8.2
5 b_all 19.9 20.8 注:19.9=10.1+9.8 20.8=12.2+8.6
6 c_all 17.6 19.4
请问如何用SQL语句或存贮过程来实现?
Try:
Select * from 表A
Union all
Select * from 表A
Union all
Select 5,'b_all',sum(field2),sum(field3) from 表A where field1 = 'b'
group by 5,'b_all'
Union all
Select 6,'b_all',sum(field2),sum(field3) from 表A where field1 = 'c'
group by 5,'b_all'
group by 5,'b_all'
Union all
Select 6,'b_all',sum(field2),sum(field3) from 表A where field1 = 'c'
group by 5,'b_all'
――――
Select * from 表A
Union all
Select (select max(id) from 表A)+1 ,'b_all' ,sum(field2),sum(field3) from 表A where field1 = 'b'
Union all
Select (select max(id) from 表A)+2,'c_all',sum(field2),sum(field3) from 表A where field1 = 'c'
Union all
Select (select max(id) from 表A)+1 ,'b_all' ,sum(field2),sum(field3) from 表A where field1 = 'b'
Union all
Select (select max(id) from 表A)+2,'c_all',sum(field2),sum(field3) from 表A where field1 = 'c'
――――
select IDENTITY(INT,1,1) ID,T.* into #temp FROM
((select Field1,Field2,Field3 from 表A Union all
select Field1+'_all',sum(Field2),sum(Field3)
from 表A group by Field1)) T
select * from #temp
drop table #temp
((select Field1,Field2,Field3 from 表A Union all
select Field1+'_all',sum(Field2),sum(Field3)
from 表A group by Field1)) T
select * from #temp
drop table #temp
―――――
测试:
create table 表A (id int,field1 char(1),field2 numeric(10,1),field3 numeric(10,1))
insert 表A select 1 ,'b', 10.1, 12.2
union all select 2 ,'b', 9.8, 8.6
union all select 3 ,'c', 12.3, 11.2
create table 表A (id int,field1 char(1),field2 numeric(10,1),field3 numeric(10,1))
insert 表A select 1 ,'b', 10.1, 12.2
union all select 2 ,'b', 9.8, 8.6
union all select 3 ,'c', 12.3, 11.2
union all select 4 ,'c', 5.3, 8.2
Select * from 表A
Union all
Select (select max(id) from 表A)+1 ,'b_all' ,sum(field2),sum(field3) from 表A where field1 = 'b'
Union all
Select (select max(id) from 表A)+2,'c_all',sum(field2),sum(field3) from 表A where field1 = 'c'
id field1 field2 field3
----------- ------ ---------------------------------------- ----------------------------------------
1 b 10.1 12.2
2 b 9.8 8.6
Select * from 表A
Union all
Select (select max(id) from 表A)+1 ,'b_all' ,sum(field2),sum(field3) from 表A where field1 = 'b'
Union all
Select (select max(id) from 表A)+2,'c_all',sum(field2),sum(field3) from 表A where field1 = 'c'
id field1 field2 field3
----------- ------ ---------------------------------------- ----------------------------------------
1 b 10.1 12.2
2 b 9.8 8.6
3 c 12.3 11.2
4 c 5.3 8.2
5 b_all 19.9 20.8
6 c_all 17.6 19.4
(所影响的行数为 6 行)
4 c 5.3 8.2
5 b_all 19.9 20.8
6 c_all 17.6 19.4
(所影响的行数为 6 行)
✧ 例四
如何把查询到的多行数据合并为一行?
楼主Chice_wxg(学)(习)2004-11-07 13:27:22 在 MS-SQL Server / 基础类 提问
✧ 我需要类似以下功能:
update table1 set data=(select line from table2)
update table1 set data=(select line from table2)
但是select如果返回多行显然无法直接进行这种操作,
请问如何解决?谢谢~~~~~~~~~
请问如何解决?谢谢~~~~~~~~~
data 和 line都是字符传。
比如:
table2中有很多记录(line)
id line
1 asdf
2 zxcv
3 lkjha
我希望最终例如:
update table1 where id=指定记录 set data=( .....不知道怎么写....)
最终:
比如:
table2中有很多记录(line)
id line
1 asdf
2 zxcv
3 lkjha
我希望最终例如:
update table1 where id=指定记录 set data=( .....不知道怎么写....)
最终:
table1中那条记录的data 字段的内容是类似"asdf zxcv lkjha"的形式。
也就是说要把table2 中的所有内容放到一个字符串中。
也就是说要把table2 中的所有内容放到一个字符串中。
--这样的更新无法用一个语句实现
--你可以这样处理
declare @s varchar(8000)
set @s=''
select @s=@s+' '+line from table2
update table1 set data=stuff(@s,1,1,'') where id=指定记录Top
--你可以这样处理
declare @s varchar(8000)
set @s=''
select @s=@s+' '+line from table2
update table1 set data=stuff(@s,1,1,'') where id=指定记录Top
5 楼zjcxc(邹建)回复于 2004-11-07 13:48:32 得分 30
--或者写个自定义函数
create function f_str()returns varchar(8000)
create function f_str()returns varchar(8000)
as
begin
declare @s varchar(8000)
set @s=''
select @s=@s+' '+line from table2
return(stuff(@s,1,1,''))
end
go
--调用函数实现更新
update table1 set data=dbo.f_str() where id=指定记录
begin
declare @s varchar(8000)
set @s=''
select @s=@s+' '+line from table2
return(stuff(@s,1,1,''))
end
go
--调用函数实现更新
update table1 set data=dbo.f_str() where id=指定记录
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论