一、行轉列 PIVOT函數,行轉列,列轉換UNPIVOThtml
select brlx as '姓名',西藥費,中成藥,中草藥 from cc_mzjzzjb_zy a
PIVOT
(
max(a.ysje) for a.zy in(西藥費,中成藥,中草藥)
)b;sql
select * from ShoppingCart as C PIVOT(count(TotalPrice) FOR [Week] IN([1],[2],[3],[4],[5],[6],[7])) AS T
https://www.cnblogs.com/linJie1930906722/p/6036714.html
https://www.cnblogs.com/wlsandwho/p/4423956.html
https://www.cnblogs.com/ylbtech/p/8028808.html
https://www.cnblogs.com/sword-successful/p/4814840.html
2.SQL XML
左外鏈接 LEFT OUTER JOIN 函數
select dm,ks from c_dept for xml pathsqlserver
select dm,ks from c_dept for xml path('Dept')spa
select dm,ks from c_dept for xml raw.net
select dm,ks from c_dept for xml autoserver
字符串解析爲表xml
https://www.cnblogs.com/lanyubaicl/p/5671966.htmlhtm
特殊字符用nvarchar類型,插入的時候字符常量前面加Nblog
create table TbN( itemno NVARCHAR(50));
INSERT INTO TbN(itemno) VALUES (N'A02.201†' );
比較簡單的生成多行數據相似select union
SELECT * FROM (VALUES(1),(2),(3)) AS V(A)
A
1
2
3
字符串拼接分解
SELECT stuff( (SELECT ',' + turnOffice FROM (
select ( select dm from c_dept t where t.ks=items) as turnOffice FROM dbo.[Split_StrByDelimiter](REPLACE( '內一科->內三科->外一科->骨科->婦科','->',','),',')
) A FOR XML PATH('')),1,1,'') as turnOffice
https://blog.csdn.net/sqlserverdiscovery/article/details/79187307
SQL Server 2016新增了string_split函數,專門用來拆分字符串。
SQL Server 2017RTM版,新增函數string_agg