2017-03-08 22 views
0

私はRow_Numberの基本を理解しますがどのようにあなたには、いくつかの条件とそれを使用しないとBY PARTITION(OVER。だから、私はcol1の値のインスタンスの数を必要とするだけでどこcol2 is not nullcol2 is nullSQL ROW_NUMBER()条件

だから、私はこれがあります。

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

を、私は、この必要があります:私はdashDの下で実行しています

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 

をB.

答えて

1

私はあなたがもしそうならcol2には ヌルとcol2がnullではないCOL1の量 あるCOL1の 量が必要であることを理解しているように、してください

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 
、これを試してみてください