计算某列的字符串相加sql语句
一个表
两列:
column1 char(12)
column2 int
举例:
column1 column2
—————————————
kk 1
gg 1
erew 2
43243 3
要求:以column2为聚合标准,把column1所有字符串相加
sql脚本:
-------------------------------------------------------------------------
create table order_car
(
column2 int null,
column1 varchar(500) null
)
insert into order_car(column1,column2)
values
(
'43243' , 3
)
insert into order_car(column1,column2)
values
(
'erew' , 2
)
insert into order_car(column1,column2)
values
(
'gg' , 1
)
insert into order_car(column1,column2)
values
(
'kk' , 1
)
----------------------------------------------------------------------
实现方法:
select column2, stuff((select ','+ltrim(column1) from order_car
where column2=b.column2 for xml path('')),1,1,'') from order_car b
****************************************************************