2017-03-08 65 views
0

我理解的Row_Number的基礎知識,但你怎麼跟某些情況下使用它。所以,我需要在col1值的實例的數量,但只有在col2 is not nullcol2 is nullSQL ROW_NUMBER()OVER(PARTITION BY與條件

所以,我有這樣的:

col1 col2 col3 
Orange x  x 
Orange x 
Orange x 
Banana   x 
Banana  
Orange   x 
Apple x 
Aplle x 
Banana x 
Orange x  x 

,我需要這樣的:

col1 col2 col3 newcol 
Orange x  x 
Orange x    3 
Orange x    3 
Banana   x 
Banana   
Orange   x 
Apple x    2 
Aplle x    2 
Banana x    1 
Orange x  x  3 

我下dashD運行B.

回答

1

據我所知,你需要COL1的 量,其中COL2是空 和COL1的數量,其中COL2不爲空 如果是的話,試試這個,請

select col1, new_col, count(1) 
from 
(
select col1, "not_null" as new_col 
from table 
where 
    col2 is not null 
union 
select col1, "is_null" as new_col 
from table 
where 
    col2 is null 
) 
group by col1, new_col 
相關問題