初始数据:
想要的结果数据:
需求:
根据“creatDate”升序排序,“type”是“新增”的,“新增列”+“苹果”;如果“type”是“删除”的,“新增列”-“苹果”。不用游标
初始数据:
假设表名为test
select distinct(b.creatDate),stuff((select ',' + a.name from test a where a.type = '新增' and b.creatDate >= a.creatDate and not exists(select 1 from test c where c.creatDate <= b.creatDate and c.creatDate >= a.creatDate and c.type = '删除' and c.name = a.name) for xml path('')),1,1,'') as addData from test b