当前位置:网站首页>SQLSERVER merges subquery data into one field
SQLSERVER merges subquery data into one field
2022-07-30 03:59:00 【emgexgb_sef】
Business scenario:
There are two tables, department table and employee table
Department table:
Department code department name
1 A
2 A
3 A
4 B
Employee list:
Employee name Department
Zhang San 1
Li Si 1
Wang Wu 1
Zhao Liu 2
Now we need a department personnel report, which requires the employees of this department to be displayed in one column, such as:
Department code department name belongs to personnel
1 A Zhang San, Li Si, Wang Wu
2 BZhao Liu
Solution:
Through the FOR XML PATH function + STUFF function of SQLSERVER, the queried data is combined and spliced, and placed in the specified field
select STUFF((select ',' + emp.name fromt_employee emp where emp.dept_id = dept.id for xml path('')),1,1,'') as emp_names from t_Department dept
Analysis:
One.
FOR XML PATH ("row name") displays the query result set as XML, and displays the results of multiple rows on the same row
--first line
……
--The second line
……
Second,
STUFF(expression,start,length,replacewith)
The STUFF function will string is inserted into another string.It deletes characters of the specified length from the beginning of the first string; then inserts the second string at the beginning of the first string.
1, expression
Expression of character data.expression can be a constant, a variable, or a field of a subquery
2, start
start is used to specify the value of the starting position of deletion and insertion.If the value of start is negative or zero, an empty string is returned.If the length of start is greater than the first expression, an empty string is returned.The type of start can also be bigint.
Note: SQLSERVER starts at 1, and a start value of 1 represents the first character.
3, length
length is used to specify the number of characters to be deleted.If the length value is negative, an empty string is returned.If length is greater than the length of the first character_expression, up to the last character in the last character_expression can be deleted.If length is zero, the insertion occurs at the start position and no characters are deleted.The type of length can also be bigint.
4, replacewith
Replace the character expression.Likewise, it can be a constant, a variable, or a field or binary field.
Replaces expression of length characters starting at start.If replacewith is NULL, characters are removed without inserting anything.
5. Return type
If expression is of character data type, character data is returned.
If expression is of binary data type, binary data is returned.
Note:
If the expression is a subquery statement, you need to add a character to the query result, as shown in the example above, otherwise the execution result will bring out the field name of the query:
name >Zhang San Li Si
This is because: directly query the field, FOR XML PATH will return the field name as the column name by default, after adding ',', the column has no name, then the natural return is, Zhang San, Li Si, ... this, then use the STUFF function to remove the first comma, you can
If there are any mistakes or omissions, please give pointers!
Let me introduce myself first. The editor graduated from Shanghai Jiaotong University in 2013. I worked in a small company and went to big factories such as Huawei and OPPO. I joined Alibaba in 2018, until now.I know that most junior and intermediate java engineers want to upgrade their skills, they often need to explore their own growth or sign up to study, but for training institutions, the tuition fee is nearly 10,000 yuan, which is really stressful.Self-learning that is not systematic is very inefficient and lengthy, and it is easy to hit the ceiling and the technology stops.Therefore, I collected a "full set of learning materials for java development" for everyone. The original intention is also very simple. I hope to help friends who want to learn by themselves but don't know where to start, and at the same time reduce everyone's burden.Add the business card below to get a full set of learning materials
边栏推荐
- Tcp编程
- OpenFeign realize load balance
- 第51篇-知乎请求头参数分析【2022-07-28】
- Nacos Configuration Center
- Flutter records and learns different animations (1)
- The difference between BGP room and ordinary room in Beijing
- Pytorch framework learning record 2 - the use of TensorBoard
- Mini Program Graduation Works WeChat Second-hand Trading Mini Program Graduation Design Finished Works (3) Background Functions
- Chapter 51 - Knowing the request header parameter analysis【2022-07-28】
- Mini Program Graduation Works WeChat Points Mall Mini Program Graduation Design Finished Products (6) Question Opening and Defense PPT
猜你喜欢

新型LaaS协议Elephant Swap给ePLATO提供可持续溢价空间

redis分布式锁的原子保证
![[ 云原生之谜 ] 云原生背景 && 定义 && 相关技术详解?](/img/eb/0cd6891fcc00d2c01ba8bd7f8d0822.png)
[ 云原生之谜 ] 云原生背景 && 定义 && 相关技术详解?

函数的底层机制

Mini Program Graduation Works WeChat Second-hand Trading Mini Program Graduation Design Finished Works (7) Interim Inspection Report

day10--install mysql on linux

逆向理论知识3【UI修改篇】

小程序毕设作品之微信积分商城小程序毕业设计成品(6)开题答辩PPT
发给你的好友,让 TA 请你吃炸鸡!
![[Switch] Protocol-Oriented Programming in Swift: Introduction](/img/7a/6db558231a77ad9e739b571cf328d6.png)
[Switch] Protocol-Oriented Programming in Swift: Introduction
随机推荐
ospf 综合实验(重发布,特殊区域)
Taobao/Tmall get the list of sold product orders API
EasyCVR启动时报错“no such file or directory”,该如何解决?
When the EasyNVR platform is cascaded to the EasyCVR, why can't the video be played after a while?
Transformation of traditional projects
LoadBalancer load balancing
Forum management system
Eureka Registry
Flink学习第一天——什么是批量、流式计算?
Flutter records and learns different animations (1)
Summary of Rpc and gRpc Introduction
Redis "super explanation!!!!!!"
Nacos cluster partition
Mini Program Graduation Works WeChat Points Mall Mini Program Graduation Design Finished Work (5) Task Book
Taobao/Tmall get Taobao store details API
小程序毕设作品之微信二手交易小程序毕业设计成品(5)任务书
The difference between BGP room and ordinary room in Beijing
New LaaS protocol Elephant Swap provides ePLATO with sustainable premium space
Nacos achieves high availability
小程序毕设作品之微信二手交易小程序毕业设计成品(8)毕业设计论文模板