zoukankan      html  css  js  c++  java
  • 表表达式,Substring, CharIndex, 多行数据变同一行的用法

    参考:

    https://www.cnblogs.com/cnki/p/9561427.html

    https://www.cnblogs.com/johnwood/p/6386613.html

    1.表1:

    CREATE TABLE [dbo].[SQLServerInfo](
        [objid] [varchar](50) NULL,
        [instancename] [varchar](50) NULL
    ) ON [PRIMARY]
    objid    instancename
    Server1.uuu    Instance1
    Server2.uuu    Instance1
    Server3.uuu    Instance1
    Server4.uuu    Instance2
    Server5.uuu    Instance3
    View Code

    2.表2:

    CREATE TABLE [dbo].[WinServers](
        [hostname] [varchar](50) NULL,
        [serverrole] [varchar](50) NULL,
        [ip] [varchar](50) NULL
    ) ON [PRIMARY]
    hostname    serverrole    ip
    Server1    role1    1.1.1.1
    Server2    role2    1.1.1.2
    Server3    role3    1.1.1.3
    Server4    role4    1.1.1.4
    Server5    role5    1.1.1.5
    View Code

    3.查询:

     With SQLServerInfoView
     as 
     (
      select a.[instancename],b.[serverrole],b.[ip]
      from [SQLServerInfo] a
      left join winservers b on substring(a.objid,0,charindex('.',a.objid,0))=b.hostname
     )
    
    select [instancename],
        [serverrole] = (
            stuff(
                (select ',' + [serverrole] from SQLServerInfoView where [instancename] = A.[instancename] for xml path('')),
                1,
                1,
                ''
            )
        ),
        [ip] = (
            stuff(
                (select ',' + [ip] from SQLServerInfoView where [instancename] = A.[instancename] for xml path('')),
                1,
                1,
                ''
            )
        )
    from SQLServerInfoView as A group by [instancename]
    View Code
  • 相关阅读:
    Lucky Permutation Triple 构造
    HDU4283:You Are the One(区间DP)
    D. Match & Catch 后缀数组
    数学选讲 orz
    D
    一步一步线段树
    湖科大校赛第三题
    最大流部分
    bfs题目集锦
    hdu1429 bfs+状态压缩
  • 原文地址:https://www.cnblogs.com/liuzhendong/p/10489257.html
Copyright © 2011-2022 走看看