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

最新文章

  1. PHP热身
  2. ASP。net treeview xml
  3. VCenter克隆虚拟机报错msg.snapshot.error-QUIESCINGERROR
  4. Pivot的SelectionChanged事件绑定到VM的Command
  5. 图解Android - Looper, Handler 和 MessageQueue
  6. [系统开发] Postfix 邮件管理系统
  7. Java:内部类
  8. JDBC之初识
  9. Android Http Server
  10. [转]iOS Tutorial – Dumping the Application Heap from Memory
  11. JAVA监听
  12. 做一个项目前搭建一个tabBar(一)框架
  13. php批量删除,批量操作
  14. 【转】java.lang.NoSuchMethodError: javax.persistence.Table.indexes()[Ljavax/persistence/Index;
  15. Django REST framework+Vue 打造生鲜超市(四)
  16. 用PULL解析器解析XML文件
  17. c/c++ 多线程 层级锁
  18. ASP.NET AJAX入门系列(4):使用UpdatePanel控件(一)
  19. 编程,将data段中的字符串转化成大写
  20. commit

热门文章

  1. 查看SELinux状态及关闭SELinux
  2. python 获得毫秒级时间戳
  3. springmvc相关配置和用法
  4. 1017 A除以B (20 分)
  5. asp.net 同时执行js事件和代码事件 导出 excel
  6. hdu6183 Color it 线段树动态开点+查询减枝
  7. poj1002 字典树+map+查询单词出现次数
  8. Oracle 查看表结构视图
  9. Git merge 和 rebase 进一步比较
  10. python-URL转jpg图片