2016-07-21 2 views
-1
create table p(pid int, st int, v int,hid int,ht varchar(2)); 
data: 
insert into p(pid,st,hid, ht,v) 
select 1 ,20160131 ,100 ,'FO' ,10 
union select 1 ,20160131 ,101 ,'FO' ,20 
union select 1 ,20160131 ,102 ,'FO' ,30 
union select 1 ,20160131 ,103 ,'ST' ,40 
union select 1 ,20160229 ,104 ,'ST' ,50 
union select 207 ,20160229 ,301 ,'ST' ,80 
union select 207 ,20160229 ,302 ,'ST' ,20 
union select 207 ,20160331 ,303 ,'FO' ,40 
union select 102 ,20160229 ,205 ,'ST' ,60 
union select 102 ,20160229 ,206 ,'ST' ,20 
union select 102 ,20160229 ,207 ,'FO' ,20 
union select 100 ,20160131 ,201 ,'ST' ,50 
union select 100 ,20160131 ,202 ,'ST' ,50 
union select 101 ,20151231 ,203 ,'ST' ,50 
union select 101 ,20151231 ,204 ,'ST' ,50 


result: 
pid v hid ht path pre_st next_st st 
1 40 103 ST 1 NULL NULL 20160131 
100 50 201 ST 1--->100 NULL NULL 20160131 
100 50 202 ST 1--->100 NULL NULL 20160131 
101 50 203 ST 1--->101 20151231 NULL 20160131 
101 50 204 ST 1--->101 20151231 NULL 20160131 
102 20 206 ST 1--->102 NULL 20160229 20160131 
102 60 205 ST 1--->102 NULL 20160229 20160131 
207 20 302 ST 1--->102--->207 NULL 20160331 20160229 
207 80 301 ST 1--->102--->207 NULL 20160331 20160229 
303 99 304 ST 1--->102--->207--->303 NULL 20160720 20160331 


I can do it in SQLServer first. 
select pid,v,hid,ht, path=cast(pid as varchar) , 
(select max(st) from p where pid = a.pid and st < a.st) as pre_st, 
(select max(st) from p where pid = a.pid and st > a.st) as next_st, 
a.st into #t1 
from p a where pid=? and st=? 
but netezza cannot support this grammer. 

매개 변수로 pid, st에 따라 수행해야합니다. 우리가 pid = 1 및 st = 20160131을 사용한다고 가정하면, 매개 변수 세트를 하나의 테이블에 저장합니다. 테이블 만들기 (pid int, st int) i (pid, st)에 삽입 1,20160131을 선택하십시오. 어떻게 위의 결과를 실현할 수 있습니까? 그냥 임시 테이블 (SQL 서버 구문에 해당)을 만들 찾고 있다면netezza sql, 어떻게 실현 되나요?

답변

0

, 다음이있다 :

create temporary table t1 as 
select pid,v,hid,ht, path=cast(pid as varchar) , 
(select max(st) from p where pid = a.pid and st < a.st) as pre_st, 
(select max(st) from p where pid = a.pid and st > a.st) as next_st, 
a.st 
from p a where pid=? and st=? 

나는 당신이 가지고있는 ? 값이 있다고 가정하는거야 ODBC 매개 변수를 사용하고 있기 때문입니다. Netezza가 무언가를 묻는 메시지를 표시하려면 you can't.