日期:2014-05-17  浏览次数:20586 次

各位高手,请教下查询结果插入表中的问题
各位高手,请教下如何用SQL实现将两个查询结果插入到一个表中,需查询每个样本编号的最后一个工序的参数信息组合成一个表,请问如何实现,最好执行时间短一点的,因为数据量可能会很大,谢谢啦!
  

例如

表 Info

序号 样本编号 工序 参数 时间
1 1 1 1.2 2012-8-1
2 2 1 1.4 2012-8-1
3 3 1 1.8 2012-8-3
4 1 1 1.6 2012-8-4
5 2 1 1.2 2012-8-5
6 3 1 1.4 2012-8-5
7 1 2 2.1 2012-8-7
8 2 2 2.3 2012-8-8
9 3 2 2.8 2012-8-9
10 1 2 1.2 2012-8-10
11 2 2 1.4 2012-8-11
12 3 2 2.1 2012-8-12
13 1 2 2.3 2012-8-13
14 2 2 1.6 2012-8-14
15 3 2 1.2 2012-8-15
16 1 3 1.4 2012-8-16
17 2 3 2.1 2012-8-17
18 3 3 2.3 2012-8-18
19 1 3 2.8 2012-8-19
20 2 3 1.2 2012-8-20
21 3 3 1.6 2012-8-21
22 1 3 1.2 2012-8-22
23 2 3 1.4 2012-8-23
24 3 3 2.1 2012-8-23

结果
表 Result

样本编号 工序1_参数 工序2_参数
1 1.6 2.3
2 1.2 1.6
3 1.4 1.2


------解决方案--------------------
SQL code

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)
*/

------解决方案--------------------
SQL code

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)