SQL ROW_NUMBER()OVER(有条件的PARTITION BY
SQL ROW_NUMBER()OVER(PARTITION BY with conditions
我了解 Row_Number
的基础知识,但在某些情况下如何使用它。因此,我需要 col1
中的值实例的数量,但仅在 col2 is not null
和 col2 is null
.
中
所以,我有这个:
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
我在 dashDB 下运行。
据我了解,您需要
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
我了解 Row_Number
的基础知识,但在某些情况下如何使用它。因此,我需要 col1
中的值实例的数量,但仅在 col2 is not null
和 col2 is null
.
所以,我有这个:
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
我在 dashDB 下运行。
据我了解,您需要 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