Bit of a strange one that I have yet to come up with an elegant solution to just yet.
Considering the following dataset:
I want to give a row number in ascending order for all numbers that are greater than 10.
Essentially I want the following resultset
So far this is what I have rolled with but it just feels like there's a better solution available:
Any ideas?
Considering the following dataset:
Code:
DECLARE @t table (
x int
);
INSERT INTO @t (x)
VALUES (1), (3), (5), (9), (15), (4), (937), (40);
Essentially I want the following resultset
Code:
x y
---- ----
1 NULL
3 NULL
4 NULL
5 NULL
9 NULL
15 1
40 2
937 3
Code:
; WITH cte AS (
SELECT x
, CASE WHEN x > 10 THEN 937 END As condition
FROM @t
)
SELECT x
, condition
, CASE WHEN condition = 937 THEN
Row_Number() OVER (PARTITION BY condition ORDER BY x ASC)
END As y
FROM cte
ORDER
BY x