Sql 行转列 STUFF

Posted imtudou

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Sql 行转列 STUFF相关的知识,希望对你有一定的参考价值。



  SELECT  DISTINCT BizID
  ,STUFF(
  (
    SELECT ',' + ApproveName
    FROM T_Common_ApproveNode
            
    WHERE BizID = A.BizID
    FOR XML PATH('')
  )
  ,1
  ,1
  ,''
 ) AS ApproveNameStr
FROM T_Common_ApproveNode AS A

BizID                           | ApproveNameStr
000835d70a99447f96b21d4b827c45d8|何磊,胡庄,陈侃,沈聪祖,殷玥,张洁4,何磊,何磊
0010f69ef6f04d0ab0118ebd336c5454|程阔,王晓康1,玄丽薇,傅燎华,刘爽1,吴万龙
001ca702982a4311bd4dd8278738a310|周薇,张国庆,张聚实,谷世友,张洁4,常春林,高超伟,谷世友
0010f69ef6f04d0ab0118ebd336c5454|程阔,王晓康1,玄丽薇,傅燎华,刘爽1,吴万龙







--输出JSON 字符串
SELECT  TOP 10 InvoiceCategory,InvoiceType FROM T_BILL_INVOICE
FOR JSON PATH('')




--输出XML
SELECT  TOP 10 InvoiceCategory,InvoiceType FROM T_BILL_INVOICE
FOR XML PATH('')





 
SELECT ''''+ CAST( AA.ProgramID AS VARCHAR(1000) )   +''','
 FROM (
 select * from (
 select a.ProgramID,
 (select top 1 c.ParentProgramID from Sys_ProgramList c where a.ParentProgramID=c.ProgramID) ParentProgramID
 from Sys_ProgramList a inner join Sys_PageMain b on a.ProgramID=b.ProgramID
 where ISNULL(a.IsShow,0)=1 and isnull(a.IsBillPage,0)=1 and b.ListProgramID<>b.ProgramID) BillMain 
 where isnull(ParentProgramID,0)<>0
) AS AA
FOR XML PATH('')


    select  STUFF((
    SELECT 
      '"businessCode":'+ cast(billno as varchar(20)) +'"'
      + ',"operatorCode":'+(select  cUserCode from Sys_User WHERE  iUserID =  Bywho)+'"'
      +',"auditStatus":PASS"'+'"'
      +',"approvalTxt":审批通过"'+'"'
      + ',"auditDate":'+ CONVERT(varchar(100), endtime, 21) +'"'
      +''
      FROM  DJLXSZ60_log  WHERE  node = 18 and nodestatus = 5 and  endtime > '2019-4-24'   order by endtime desc 
      for xml path(''), type
    ).value('.', 'varchar(max)'), 1, 1, '')  


以上是关于Sql 行转列 STUFF的主要内容,如果未能解决你的问题,请参考以下文章

SQL Server 行转列,列转行。多行转成一列

oracle行转列sql怎么写?

SQLSERVER有张表,需要每晚自动实现行转列。

MySQL行转列与列转行

ORAClE sql如何实现行转列?

行转列的处理。