【问题标题】:netezza sql, how to come true?netezza sql,如何实现?
【发布时间】:2016-07-21 07:59:22
【问题描述】:
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,我会将参数集保存在一张表中。 创建表 i(pid int, st int) 插入 i(pid,st) 选择 1,20160131 以上结果如何实现?

【问题讨论】:

    标签: netezza


    【解决方案1】:

    如果您只是想创建一个临时表(相当于您的 SQL Server 语法),那么就是这样:

    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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-04-12
      • 2016-11-17
      • 1970-01-01
      • 1970-01-01
      • 2016-12-06
      • 2019-02-11
      • 2015-09-28
      相关资源
      最近更新 更多