2014-10-20 40 views
-1

我使用下面的代码,以匹配ID号行:SQL - 创建于PARTITION层次BY语句

SELECT *, 
    LEAD(ORDER, 1) OVER(PARTITION BY ID_NBR) AS PREV_ORDER 
FROM TABLE 

在我的数据,一些身份证号码丢失。如果(且仅当)ID号缺失,我想通过Cust_Name来匹配行。 LEAD/OVER命令有可能吗?

谢谢,

+0

失踪之前,你的意思是他们是空? – radar 2014-10-20 19:39:41

+0

ID_NBR字段为空。 – waealu 2014-10-20 19:41:51

+0

你能定义“匹配”吗?什么是'cust_name'?列'订单'在哪里进入?你知道'order'是一个非法列名吗?如果您发布了原始数据的样本,然后再添加另一个您想要的数据样本,则可能会使事情变得更加简单,同时还会详细描述如何将数据转换为其他数据。 – Ben 2014-10-20 20:23:21

回答

0

也许你需要类似的东西?

with tab as (
select 1 id, 'n1' cust_name from dual 
union all select 2, 'n2' from dual 
union all select null, 'n3' from dual 
union all select 4, 'n4' from dual 
) 
select id, cust_name, 
      lag(cust_name ignore nulls) over(order by id) prev_name, 
      lead(cust_name ignore nulls) over(order by id) next_name 
from tab; 

IGNORE NULLS - 在查找滞后/前导值时跳过NULL值。

您可以添加 “PARTITION BY山坳[山口]” ORDER BY

它必须有ORDER BY与LAG/LEAD功能