日期:2014-05-17 浏览次数:21308 次
if OBJECT_ID('tempdb..#temp', 'u') is not null drop table #temp;
go
create table #temp( [单号] varchar(100), [审核人] varchar(100), [流程节点] int);
insert #temp
select 'A001','张三','2001' union all
select 'A001','李四','2002' union all
select 'A001','陈五','2003' union all
select 'A001','王六','2004' union all
select 'A002','张三','2001' union all
select 'A002','李四','2002' union all
select 'A002','陈五','2003' union all
select 'A002','王六','2004'
--为什么不能用聚合呢?
--方法1.CASE WHEN:
SELECT
[单号],
[一审(2001)] = MAX(CASE [流程节点] WHEN 2001 THEN [审核人] END),
[二审(2002)] = MAX(CASE [流程节点] WHEN 2002 THEN [审核人] END),
[三审(2003)] = MAX(CASE [流程节点] WHEN 2003 THEN [审核人] END),
[四审(2004)] = MAX(CASE [流程节点] WHEN 2004 THEN [审核人] END)