1. 怎么用sql语句将一张表中ID相同的行的内容合并在一起
工具/材料:Management Studio。
1、首先在桌面上,点击“Management Studio”图标。
2. SQL 将属于同一个ID下的多行数据合并到一行
selectid,sum(isnull(data1,0))data1,sum(isnull(data2,0))data2from表1groupbyid
sqlserver写法,其他数据库略有不同
3. sql语句如何合并相同id下的多行数据在一行 例如:
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 ) result_2,
max(case when rn = 2 then else end ) else_2
from (
select a.*,
row_number() over(partition by id order by else) rn
from a)
group by id;
4. 请教SQL可以把ID相同的两行数据合并到一起吗
create Function GetName12(@id char(10))
Returns Nvarchar(2000)
As
Begin
Declare
@S Nvarchar(2000)
Set @S=''
Select @S=@S+','+name from 表1 Where id=@id Order By name
Return (Stuff(@S,1,1,'')) End
--select id, dbo.GetName12( id) as Field from 表1
5. sql查询结果中有id相同的字段如何对查询结果进行操作将id相同的合并为一条数据
也不说你是用的什么数据库,sqlserver为例
建表
createtableclass
(classidint,
classnamevarchar(10),
teacheridint)
insertintoclassvalues(1,'一班',1)
insertintoclassvalues(1,'一班',2)
insertintoclassvalues(2,'三班',1)
createtableteacher
(teacheridint,
teachernamevarchar(10))
insertintoteachervalues(1,'zhangsan')
insertintoteachervalues(2,'lisi')
执行
withtbas
(selecta.classid,a.classname,b.teachername
fromclassaleftjointeacherb
ona.teacherid=b.teacherid)
selectclassid,classname,teachername=stuff((select','+=tb.classidandclassname=tb.classnameforxmlpath('')),1,1,'')
fromtb
groupbyclassid,classname
结果截图
6. sql语句查出多行数据,如何将ID相同的行并成一行,且不相同的字段合成一个字符串
我个人建议你把逻辑写在代码里面
像这种数据库操作很好资源的,严重影响效率
可以先取出一个list
List<User> resultList = 数据库返回
Map<String,String> map = new HashMap<String,String>();
for(User user : resultList){
String val ;
if(map.containsKey(user.getID().toString())){
val = map.get(user.getID().toString());
val = val + user.getAnotherItem();
map.remove(user.getID().toString());
}else{
val = user.getAnotherItem();
}
map.put(user.getID().toString(),val);
}
//map里面的东西就是你要的
7. SQL 如何将一个表中的两条或多条拥有相同ID的记录合并为一条
一、创建表:
create table stuUnion
(
sid int identity primary key,
cid int,
id varchar(500)
)
二、添加数据:
insert into stuUnion
elect 1,'a' union
select 1,'b' union
select 2,'c' union
select 2,'d' union
select 3,'e' union
select 3,'f' union
select 3,'g'
三、用标量函数查询:
创建标量函数:
create function b(@cid int)
returns varchar(500)
as
begin
declare @s varchar(500)
select @s=isnull(@s+'','')+rtrim(id)+',' from stuUnion where cid=@cid
return @s
end;
用标量函数查询:
select cid,dbo.b(cid) as id from stuUnion group by cid
用sqlserver的xml:
select cid,ID=STUFF((select ' '+rtrim(id)+',' from stuUnion where st.cid=cid order by id for XML path('')),1,1,'') from stuUnion st group by cid
8. sql语句,合并多条记录中的相同字段。
假如你的表结构如下:
create table tb_test
(
商店id int,
时间id int,
用户id int,
购买产品id int
)
可以创建如下聚合函数:
create function fn_test(@商店id int,@时间id int,@用户id int)
returns varchar(8000)
as
begin
declare @ret varchar(8000)
set @ret=''
select @ret=@ret+convert(varchar(100),购买产品id int)+',' from tb_test where 商店id=@商店id and 时间id=@时间id and 用户id=@用户id
select @ret=left(@ret,len(@ret)-1)
return(@ret)
end
然后用如下SQL语句进行查询即可。
(@商店id int,@时间id int,@用户id int
select 商店id,时间id,用户id,dbo.fn_test(商店id,时间id,用户id)as 购买产品id
from tb_test
group by 商店id,时间id,用户id
还有一种更简单的办法,在2005上测试没有问题,2008上可自己验证一下:
select
商店id,时间id,用户id,
stuff((select ','+convert(varchar(10),购买产品id) from tb_test b
where 商店id=a.商店id and 时间id=a.时间id and 用户id=a.用户id
for xml path('')),1,1,'')
from tb_test a
group by 商店id,时间id,用户id