![]() | |
![]() |
| | Thread Tools | Display Modes |
#1
| |||
| |||
|
#2
| |||
| |||
|
#3
| |||
| |||
|
#4
| |||
| |||
|
|
Tonkuma, interesting solution. Is there a quick way to skip null values from DENSE_RANK, without having to do a query on top of the results ? Thanks, -M I couldn't understand the case of need to consider null value for |
#5
| |||
| |||
|
#6
| |||
| |||
|
#7
| |||
| |||
|
|
If all values of the count column in a partition were null, previous olap expression would return 1. I found another expression which worked even if all values of the count column in a partition were null, ------------------------------ Commands Entered ------------------------------ WITH *b(id, name) AS ( VALUES (CAST(NULL AS INT), 'A'), (CAST(NULL AS INT), 'A') , (2, 'B'), (CAST(NULL AS INT), 'A'), (CAST(NULL AS INT), 'B') , (5, 'C'), (CAST(NULL AS INT), 'C') , (5, 'C'), (CAST(NULL AS INT), 'C'), (7, 'C') ) SELECT name * * *, COUNT( CASE ROW_NUMBER() * * * * * * * * * * *OVER(PARTITION BY name , id) * * * * * * * WHEN 1 THEN id * * * * * * * END * * * * * * ) * * * * *OVER(PARTITION BY name) AS count_distinct_id_2 * * *, COUNT(id) * * * * *OVER(PARTITION BY name) AS count_id * * *, COUNT(*) * * * * *OVER(PARTITION BY name) AS count_rows * * *, id *FROM *b *ORDER BY * * * *name ; ------------------------------------------------------------------------------ NAME COUNT_DISTINCT_ID_2 COUNT_ID * *COUNT_ROWS *ID ---- ------------------- ----------- ----------- ----------- A * * * * * * * * * * *0 * * * * * 0 * * * * * 3 * * * * * - A * * * * * * * * * * *0 * * * * * 0 * * * * * 3 * * * * * - A * * * * * * * * * * *0 * * * * * 0 * * * * * 3 * * * * * - B * * * * * * * * * * *1 * * * * * 1 * * * * * 2 * * * * * 2 B * * * * * * * * * * *1 * * * * * 1 * * * * * 2 * * * * * - C * * * * * * * * * * *2 * * * * * 3 * * * * * 5 * * * * * 5 C * * * * * * * * * * *2 * * * * * 3 * * * * * 5 * * * * * 5 C * * * * * * * * * * *2 * * * * * 3 * * * * * 5 * * * * * 7 C * * * * * * * * * * *2 * * * * * 3 * * * * * 5 * * * * * - C * * * * * * * * * * *2 * * * * * 3 * * * * * 5 * * * * * - * 10 record(s) selected. |
#8
| |||
| |||
|
#9
| |||
| |||
|
|
If all values of the count column in a partition were null, previous olap expression would return 1. I found another expression which worked even if all values of the count column in a partition were null, |
![]() |
| Thread Tools | |
| Display Modes | |
| |