[英]SQL ROW_NUMBER()OVER(PARTITION BY with conditions
I understand the basics of Row_Number
but how do you use it with some conditions. 我了解
Row_Number
的基础知识,但是在某些条件下如何使用它。 So, I need the number of an instance of a value in col1
BUT only where col2 is not null
and col2 is null
. 因此,仅在
col2 is not null
且col2 is null
的情况下,我只需要col1
BUT中值实例的编号。
So, I have this: 所以,我有这个:
col1 col2 col3
Orange x x
Orange x
Orange x
Banana x
Banana
Orange x
Apple x
Aplle x
Banana x
Orange x x
and I need this: 我需要这个:
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
I am running under dashDB. 我在dashDB下运行。
As I understand that you need quantity of col1 where col2 is null and quantity of col1 where col2 is not null If so, try this, please 据我了解,您需要col1为null的col1数量和col2不为null的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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.