Given a set of rows, with a field sometimes null
and sometimes not:
SELECT
Date, TheThing
FROM MyData
ORDER BY Date
Date TheThing
----------------------- --------
2016-03-09 08:17:29.867 a
2016-03-09 08:18:33.327 a
2016-03-09 14:32:01.240 NULL
2016-10-21 19:53:49.983 NULL
2016-11-12 03:25:21.753 b
2016-11-24 07:43:24.483 NULL
2016-11-28 16:06:23.090 b
2016-11-28 16:09:07.200 c
2016-12-10 11:21:55.807 c
I want to have a ranking column that counts the non-null values:
Date TheThing DesiredTotal
----------------------- -------- ------------
2016-03-09 08:17:29.867 a 1
2016-03-09 08:18:33.327 a 2
2016-03-09 14:32:01.240 NULL 2 <---notice it's still 2 (good)
2016-10-21 19:53:49.983 NULL 2 <---notice it's still 2 (good)
2016-11-12 03:25:21.753 b 3
2016-11-24 07:43:24.483 NULL 3 <---notice it's still 3 (good)
2016-11-28 16:06:23.090 b 4
2016-11-28 16:09:07.200 c 5
2016-12-10 11:21:55.807 c 6
I try the obvious:
SELECT
Date, TheThing,
RANK() OVER(ORDER BY Date) AS Total
FROM MyData
ORDER BY Date
But RANK()
counts nulls:
Date TheThing Total
----------------------- -------- -----
2016-03-09 08:17:29.867 a 1
2016-03-09 08:18:33.327 a 2
2016-03-09 14:32:01.240 NULL 3 <--- notice it is 3 (bad)
2016-10-21 19:53:49.983 NULL 4 <--- notice it is 4 (bad)
2016-11-12 03:25:21.753 b 5 <--- and all the rest are wrong (bad)
2016-11-24 07:43:24.483 NULL 7
2016-11-28 16:06:23.090 b 8
2016-11-28 16:09:07.200 c 9
2016-12-10 11:21:55.807 c 10
How can i instruct RANK()
(or DENSE_RANK()
) to not count nulls?
Have you tried using a PARTITION?
Why yes! Much worse:
SELECT
Date, TheThing,
RANK() OVER(PARTITION BY(CASE WHEN TheThing IS NOT NULL THEN 1 ELSE 0 END) ORDER BY Date) AS Total
FROM MyData
ORDER BY Date
But RANK()
counts nulls:
Date TheThing Total
----------------------- -------- -----
2016-03-09 08:17:29.867 a 1
2016-03-09 08:18:33.327 a 2
2016-03-09 14:32:01.240 NULL 1 <--- reset to 1?
2016-10-21 19:53:49.983 NULL 2 <--- why go up?
2016-11-12 03:25:21.753 b 3
2016-11-24 07:43:24.483 NULL 3 <--- didn't reset?
2016-11-28 16:06:23.090 b 4
2016-11-28 16:09:07.200 c 5
2016-12-10 11:21:55.807 c 6
And now i randomly type things - frantic flailing.
SELECT
Date, TheThing,
RANK() OVER(PARTITION BY(CASE WHEN TheThing IS NOT NULL THEN 1 ELSE NULL END) ORDER BY Date) AS Total
FROM MyData
ORDER BY Date
SELECT
Date, TheThing,
DENSE_RANK() OVER(PARTITION BY(CASE WHEN TheThing IS NOT NULL THEN 1 ELSE NULL END) ORDER BY Date) AS Total
FROM MyData
ORDER BY Date
Edit: With all the answers, it took many iterations to find all the edge cases that i don't want. In the end what i conceptually wanted was OVER()
in order to count. I had no idea OVER
applied to anything other than RANK
(and DENSE_RANK
).
http://sqlfiddle.com/#!18/c6d87/1