• sql多行合并成一行用逗号隔开,多表联合查询中子查询取名可重复


    简单版的

    SELECT a.CreateBy,
    Name =stuff(
    (
    select ','+Name FROM SG_Client WHERE CreateBy = a.CreateBy for xml path(''))
    ,1,1,'')
    FROM SG_Client a 
    group by a.CreateBy

    //连表查询

    SELECT a.ContractID, LocationName =
    stuff( 
    ( select ',' + LocationName FROM(select L.Name as LocationName,b.ContractID from SG_ContractBunk B inner join MALL_Location L 
    on B.LocationID = L.LocationID ) tb where tb.ContractID=a.ContractID for xml path('')) ,1,1,'') 
    FROM(select L.Name as LocationName,b.ContractID from SG_ContractBunk B inner join MALL_Location L on B.LocationID = L.LocationID
    ) a group by a.ContractID
    select C.ContractID,ContractNO,ContractNO2,ContractName,State,CL.Name as ClientName,B.Name as BrandName,l.LocationName from SG_Contract C WITH (NOLOCK) 
    inner join SG_Client CL WITH (NOLOCK) on C.ClientID=CL.ClientID inner join SG_Brand B WITH (NOLOCK) on c.BrandID=B.BrandID 
    inner join (SELECT a.ContractID, LocationName =
    stuff( 
    ( select ',' + LocationName FROM(select L.Name as LocationName,b.ContractID from SG_ContractBunk B inner join MALL_Location L 
    on B.LocationID = L.LocationID ) tb where tb.ContractID=a.ContractID for xml path('')) ,1,1,'') 
    FROM(select L.Name as LocationName,b.ContractID from SG_ContractBunk B inner join MALL_Location L on B.LocationID = L.LocationID
    ) a group by a.ContractID) L ON C.ContractID=L.ContractID
  • 相关阅读:
    RTTI机制
    constexpr
    map/unordered_map
    Centos 安装Oracle
    JS带进度 文件 重复 自动 异步上传
    xadmin 小组件默认折叠
    xadmin datetime 类型报错 unsupported format characte
    Vmware 链接克隆 转 完整克隆 Converting a linked clone virtual machine to a full clone virtual machine
    vsftpd 530 500 553
    百度自然语言处理API调用
  • 原文地址:https://www.cnblogs.com/heyiping/p/9402833.html
Copyright © 2020-2023  润新知