zoukankan      html  css  js  c++  java
  • SQL之将多列合并一列

    模板
    select rent_id,
                 devicesn = (stuff((select ',' + devicesn from  () AS a where a.rent_id =   
    b.rent_id for xml path('')),1,1,''))
     from  () AS b group by  rent_id
    
    

    实例

    select rent_id,devicename = (stuff((select ',' + devicename from  (SELECT rd.rent_id, mc.name AS devicename,m.name AS devicemodel,ms.sn_number as devicesn 
    FROM dbo.material_category mc INNER JOIN 
      dbo.material m ON m.category_id = mc.category_id
      INNER JOIN dbo.material_sn ms ON ms.material_id = m.material_id
      INNER JOIN dbo.rent_detail rd ON rd.sn_number = ms.sn_number) AS a where a.rent_id =   
    b.rent_id for xml path('')),1,1,'')),
                 devicemodel = (stuff((select ',' + devicemodel from  (SELECT rd.rent_id, mc.name AS devicename,m.name AS devicemodel,ms.sn_number as devicesn 
    FROM dbo.material_category mc INNER JOIN 
      dbo.material m ON m.category_id = mc.category_id
      INNER JOIN dbo.material_sn ms ON ms.material_id = m.material_id
      INNER JOIN dbo.rent_detail rd ON rd.sn_number = ms.sn_number) AS a where a.rent_id =   
    b.rent_id for xml path('')),1,1,'')),
                 devicesn = (stuff((select ',' + devicesn from  (SELECT rd.rent_id, mc.name AS devicename,m.name AS devicemodel,ms.sn_number as devicesn 
    FROM dbo.material_category mc INNER JOIN 
      dbo.material m ON m.category_id = mc.category_id
      INNER JOIN dbo.material_sn ms ON ms.material_id = m.material_id
      INNER JOIN dbo.rent_detail rd ON rd.sn_number = ms.sn_number) AS a where a.rent_id =   
    b.rent_id for xml path('')),1,1,''))
     from  (SELECT rd.rent_id, mc.name AS devicename,m.name AS devicemodel,ms.sn_number as devicesn 
    FROM dbo.material_category mc INNER JOIN 
      dbo.material m ON m.category_id = mc.category_id
      INNER JOIN dbo.material_sn ms ON ms.material_id = m.material_id
      INNER JOIN dbo.rent_detail rd ON rd.sn_number = ms.sn_number) AS b group by  rent_id
  • 相关阅读:
    C语言I博客作业04
    C语言I博客作业02
    The First Assignment
    蒟蒻的长链剖分学习笔记(例题:HOTEL加强版、重建计划)
    分治FFT模板
    [Ynoi2016]掉进兔子洞 题解
    蒟蒻首开博客园博客QwQ
    bzoj4320 homework 题解
    [ZJOI2016]小星星(容斥+dp)
    【scoi2009】围豆豆(最短路模型)
  • 原文地址:https://www.cnblogs.com/weixin18/p/9758937.html
Copyright © 2011-2022 走看看