日期:2014-05-17 浏览次数:20586 次
create table Info (序号 int, 样本编号 int, 工序 int, 参数 decimal(5,1), 时间 date) insert into Info select 1, 1, 1, 1.2, '2012-8-1' union all select 2, 2, 1, 1.4, '2012-8-1' union all select 3, 3, 1, 1.8, '2012-8-3' union all select 4, 1, 1, 1.6, '2012-8-4' union all select 5, 2, 1, 1.2, '2012-8-5' union all select 6, 3, 1, 1.4, '2012-8-5' union all select 7, 1, 2, 2.1, '2012-8-7' union all select 8, 2, 2, 2.3, '2012-8-8' union all select 9, 3, 2, 2.8, '2012-8-9' union all select 10, 1, 2, 1.2, '2012-8-10' union all select 11, 2, 2, 1.4, '2012-8-11' union all select 12, 3, 2, 2.1, '2012-8-12' union all select 13, 1, 2, 2.3, '2012-8-13' union all select 14, 2, 2, 1.6, '2012-8-14' union all select 15, 3, 2, 1.2, '2012-8-15' union all select 16, 1, 3, 1.4, '2012-8-16' union all select 17, 2, 3, 2.1, '2012-8-17' union all select 18, 3, 3, 2.3, '2012-8-18' union all select 19, 1, 3, 2.8, '2012-8-19' union all select 20, 2, 3, 1.2, '2012-8-20' union all select 21, 3, 3, 1.6, '2012-8-21' union all select 22, 1, 3, 1.2, '2012-8-22' union all select 23, 2, 3, 1.4, '2012-8-23' union all select 24, 3, 3, 2.1, '2012-8-23' select a.样本编号, max(case when a.工序=1 then 参数 else -1 end) '工序1_参数', max(case when a.工序=2 then 参数 else -1 end) '工序2_参数' into Result from Info a inner join (select 样本编号,工序,max(序号) md from Info group by 样本编号,工序) b on a.样本编号=b.样本编号 and a.序号=b.md group by a.样本编号 select * from Result /* 样本编号 工序1_参数 工序2_参数 ----------- ----------------- ------------------- 1 1.6 2.3 2 1.2 1.6 3 1.4 1.2 (3 row(s) affected) */
------解决方案--------------------
create table Info (序号 int, 样本编号 int, 工序 int, 参数 decimal(5,1), 时间 date) insert into Info select 1, 1, 1, 1.2, '2012-8-1' union all select 2, 2, 1, 1.4, '2012-8-1' union all select 3, 3, 1, 1.8, '2012-8-3' union all select 4, 1, 1, 1.6, '2012-8-4' union all select 5, 2, 1, 1.2, '2012-8-5' union all select 6, 3, 1, 1.4, '2012-8-5' union all select 7, 1, 2, 2.1, '2012-8-7' union all select 8, 2, 2, 2.3, '2012-8-8' union all select 9, 3, 2, 2.8, '2012-8-9' union all select 10, 1, 2, 1.2, '2012-8-10' union all select 11, 2, 2, 1.4, '2012-8-11' union all select 12, 3, 2, 2.1, '2012-8-12' union all select 13, 1, 2, 2.3, '2012-8-13' union all select 14, 2, 2, 1.6, '2012-8-14' union all select 15, 3, 2, 1.2, '2012-8-15' union all select 16, 1, 3, 1.4, '2012-8-16' union all select 17, 2, 3, 2.1, '2012-8-17' union all select 18, 3, 3, 2.3, '2012-8-18' union all select 19, 1, 3, 2.8, '2012-8-19' union all select 20, 2, 3, 1.2, '2012-8-20' union all select 21, 3, 3, 1.6, '2012-8-21' union all select 22, 1, 3, 1.2, '2012-8-22' union all select 23, 2, 3, 1.4, '2012-8-23' union all select 24, 3, 3, 2.1, '2012-8-23' select max(case when a.工序=1 then a.序号 else -1 end) '序号', a.样本编号, max(case when a.工序=1 then 参数 else -1 end)