Sample code for merging group information using XML FOR PATH (picture and text)

黄舟
Release: 2017-03-20 16:35:41
Original
1593 people have browsed it


Recently, when doing statistical functions, I often need to use the mergegrouping content. If I only use the aggregationfunction based on the grouped statistical values, that’s it. , if we process the grouped string columns, we must write our own function to process it. For example, if there is such data information: To group the above data and obtain statistical results:

                                                                                                                                                      Method of generating XML
using SQL technology It’s so troublesome to handle functions. ​​​​​​

​​​​​
Extension: For XML Path
​​​​​​​​​​ Because the SELECT clause does not specify any column name aliases, the resulting subelement names are the same as the corresponding column names in the SELECT clause. If no information is specified for path, a <
row
> tag will be added for each row in the rowset.
      

        SQL statement:

-- ================================================
-- Description:合并分组内容
-- Author:夏保华
-- Date:2009-08-06
-- ================================================
create   table   Employees(DepartmentName varchar(50),EmpoyeeName  varchar(20))   
insert into Employees   
select '开发部','小刘' union all
select '开发部','小王' union all
select '开发部','小张' union all
select '工程部','老吴' union all
select '工程部','老李' union all
select '市场部','大兵' union all
select '市场部','大黄' union all
select '市场部','大虾' union all
select '市场部','大国'
go 

create function  Sum_ByGroup(@DepartmentName varchar(50))   
returns varchar(8000)   
as   
begin   
    declare @ret varchar(8000)   
    set   @ret  =  ''   
    select  @ret  =  @ret+','+EmpoyeeName from Employees where DepartmentName = @DepartmentName   
    set   @ret   =   stuff(@ret,1,1,'')   
    return   @ret     
end   
go

select DepartmentName,dbo.Sum_ByGroup(DepartmentName) as EmployeesList from Employees
group by DepartmentName
go
Copy after login


                                                                                                                Overrides the default <

row

>. For example, the following query will return the corresponding <

Employee

> element for each row in the rowset.
     
       SQL statement:

select
 DepartmentName,
stuff
((
select
 
'
,
'
+
EmpoyeeName 
from
 Employees 
where
 DepartmentName 
=
 e.DepartmentName 
for
 xml path(
''
)),
1
,
1
,
''
) 
as
 EmployeesList 
from
 Employees E
group
 
by
 DepartmentName
Copy after login



##                                                                                                                                 # 3. If a zero-length string is specified, no wrapping elements will be generated.
Result:

4. You can add a single top-level element by specifying the
root
option in the FOR XML SQL statement:

select
 DepartmentName,(
select
 
''
+
EmpoyeeName 
from
 Employees 
where
 DepartmentName 
=
 e.DepartmentName 
for
 xml path) 
as
 EmployeesList 
from
 Employees E
group
 
by
 DepartmentName
Copy after login
## Result:


The above is the detailed content of Sample code for merging group information using XML FOR PATH (picture and text). For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!