SQL对多个列分别进行合并的操作,100分送上谢谢
操作了一个查询,大概联合了20张表左右.现在需要对一些数据做一些处理,因为部分不相同的列,需要合并在一起,并追加分隔号
这是查询出来的数据段
其中,标注的Order_No,Insp_type,Style_Name,Type_Desc都可能存在不相同的数据,所以,需要把这些列都合并起来,加上分割号,大概如下图效果
这是合并之后的数据
合并的代码如下
SELECT B.Agreement_Transaction_Id,B.Agreement_Transaction_No,B.Customer_Name,LEFT(AgreementList,LEN(AgreementList)-1) as Order_No FROM (
SELECT Agreement_Transaction_Id,Agreement_Transaction_No,Customer_Name,
(SELECT Order_No+';' FROM V_Agreement_Transaction_Invoice WHERE Agreement_Transaction_Id=A.Agreement_Transaction_Id FOR XML PATH('')) AS AgreementList
FROM V_Agreement_Transaction_Invoice A
GROUP BY Agreement_Transaction_Id,Agreement_Transaction_No,Customer_Name)B
SELECT Agreement_Transaction_Id,
Agreement_Transaction_No,
Customer_Name,
stuff((SELECT distinct ';'+Order_No FROM V_Agreement_Transaction_Invoice
WHERE Agreement_Transaction_Id=A.Agreement_Transaction_Id
FOR XML PATH('')
),1,1,'') AS Order_No,
stuff((SELECT distinct ';'+Inspection_type FROM V_Agreement_Transaction_Invoice
WHERE Agreement_Transaction_Id=A.Agreement_Transaction_Id
FOR XML PATH('')
),1,1,'') AS Inspection_type,
stuff((SELECT distinct ';'+Style_name FROM V_Agreement_Transaction_Invoice
WHERE Agreement_Transaction_Id=A.Agreement_Transaction_Id
FOR XML PATH('')
),1,1,'') AS Style_name,
stuff((SELECT distinct ';'+Type_description FROM V_Agreement_Transaction_Invoice
WHERE Agreement_Transaction_Id=A.Agreement_Transaction_Id
FOR XML PATH('')
),1,1,'') AS Type_description
FROM V_Agreement_Transaction_Invoice A
GROUP BY Agreement_Transaction_Id,Agreement_Transaction_No,Customer_Name