如何将此查询编写为连接而不是相关查询?

时间:2015-04-29 17:53:10

标签: sql netezza ansi-sql

因此,Netezza无法在SELECT a_id, year, count(*) FROM table1 GROUP BY a_id, year ORDER BY a_id, year 语句中使用相关子查询,这很不幸在我的特定情况下我无法想到避免这种情况的单一方法。我正在考虑用SELECT做点什么;但是,我不能在ROW_NUMBER()子句中包含窗口函数。

我有以下查询:

HAVING

有什么建议吗?

1 个答案:

答案 0 :(得分:3)

这应返回预期结果:

select *
from 
 (
   select 
      a.*
     ,b.col1 as b_col1
     ,row_number() 
      over (partition by a.ky
            order by b.date desc NULLS FIRST) as rn 
   from a left join b
   where b.ky = a.ky 
   and a.date <= b.date
 ) as dt
where rn = 1