zoukankan      html  css  js  c++  java
  • sqlServer拼结列字符串

    with table1(sessionID,message,createTime)
    as
    (
    select 1 ,'hello' ,'2014/5/6' union all
    select 1 ,'word' ,'2015/6/5' union all
    select 1 ,'你好' ,'2015/7/4' union all
    select 2 ,'hello' ,'同上时间' union all
    select 2 ,'你好' ,'同上时间'
    )
    select sessionID,
    replace(cast((select ','+message from table1 t2
    where t1.sessionID = t2.sessionID
    for xml path('')) as varchar(100)),',','') as message,
    max(createtime) createtime
    from table1 t1
    group by sessionID;

    WITH t AS (SELECT sec,orgCode, TYPE,
    replace(cast((select ','+sec from Columntype t2
     where t1.orgCode = t2.orgCode AND t1.type=t2.type
     for xml path('')) as varchar(100)),',','&') as ColumnCode
    FROM Columntype t1
      WHERE ISNULL(sec,'')<>'' GROUP BY TYPE,orgCode,sec
     ),
     tr AS ( SELECT orgCode, TYPE, ColumnCode, RIGHT(ColumnCode,LEN(ColumnCode)-1) AS trs FROM t
     ),
     ty AS ( SELECT DISTINCT TYPE,orgCode,trs FROM tr
     ),
     tu AS (SELECT DISTINCT t.*,ct.orgname,ct.typeOrgCode FROM ty t LEFT JOIN Columntype ct ON t.orgCode=ct.orgCode
      WHERE t.orgCode=ct.orgCode AND t.type=ct.type
     )
     SELECT trs,typeOrgCode,tu.orgname FROM tu WHERE TYPE='dept' ORDER BY tu.orgname

  • 相关阅读:
    获取窗口句柄,并向窗口发送自定义消息
    双向链表总结
    循环链表总结
    顺序队列总结
    链式栈总结
    顺序栈的总结
    链式队列总结
    源码网址
    通用型动态数组的总结
    单链表的链式存储总结
  • 原文地址:https://www.cnblogs.com/furenjian/p/9459374.html
Copyright © 2011-2022 走看看