-5

这是上一个查询 ( select Well_Dprod_Date,Formation_Name from data_dprod) 的结果。

 Well_Dprod_Date    Formation_Name  
 11/15/2002        BRF. 
 11/16/2002        BRF. 
 11/17/2002        BRF. 
 11/18/2002        BRF. 
 11/19/2002         BRF.    
 11/20/2002         BRF.    
 11/21/2002         BRF.    
 11/22/2002         BRF.    
 11/23/2002         BRF.    

如何获取查询结果如下:

 Well_Dprod_Date    Formation_Name  
 11/15/2002         BRF.        
 11/16/2002     
 11/17/2002     
 11/18/2002     
 11/19/2002     
 11/20/2002     
 11/21/2002     
 11/22/2002     
 11/23/2002          BRF.   

谢谢。

4

1 回答 1

0

你的问题有点模棱两可。是你想要的吗?

SELECT WELL_DPROD_DATE,
       FORMATION_NAME,
       CASE
         WHEN R1 = 1 OR R2 = 1 THEN
          FORMATION_NAME
         ELSE
          NULL
       END AS FORMATION_NAME_F
 FROM (SELECT WELL_DPROD_DATE,
           FORMATION_NAME,
           RANK() OVER(PARTITION BY FORMATION_NAME ORDER BY WELL_DPROD_DATE ASC) AS R1,
           RANK() OVER(PARTITION BY FORMATION_NAME ORDER BY WELL_DPROD_DATE DESC) AS R2
          FROM DATA_DPROD) T
于 2013-04-17T03:00:39.040 回答