-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrownumber_example.sql
84 lines (72 loc) · 1.64 KB
/
rownumber_example.sql
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
--Let's create a test database
CREATE DATABASE TestDB
GO
USE TestDB
GO
-- Create a table to hold Students
CREATE TABLE dbo.Students
(
StudentName VARCHAR(50)
, Grade INT -- We don't care about decimals
)
GO
-- Some variables to handle our business ;)
DECLARE @student VARCHAR(50) = ''
, @counter INT = 1
, @max_students INT = 25 -- Just 25 students for our example
, @max_grade INT = 100 -- No one can get a grade greater than 100, except for Chuck Norris
, @min_grade INT = 44 -- Let's suppose nobody got less than 45
WHILE @counter <= @max_students
BEGIN
INSERT INTO dbo.Students(StudentName, Grade)
SELECT
'Student' + CAST(@counter AS VARCHAR) StudentName
, FLOOR(RAND()*(@max_grade-@min_grade+1))+@min_grade AS Grade
-- This formula helps you create random numbers within a range, in this case between 45 and 100
SET @counter += 1
END
-- This query gives us the result we want
SELECT TOP (1)
StudentName
, Grade
FROM
(
SELECT TOP (2)
StudentName
, Grade
FROM
Students
ORDER BY
Grade DESC
) A
ORDER BY Grade ASC
GO
-- If we execute the inner query as a separate, we see that Student10 is actually the second place with a grade of 96
SELECT TOP (2)
StudentName
, Grade
FROM
Students
ORDER BY
Grade DESC
-- Create a CTE
;WITH StudentsCTE
AS
(
-- Syntax ROW_NUMBER()
SELECT
StudentName
, Grade
, ROW_NUMBER() OVER(ORDER BY Grade DESC) AS RowNumber
-- Withouot PARTITION BY, it will use the entire set of rows
FROM
Students
)
SELECT
* -- Here's fine to use * because we selected what we wanted inside the CTE
FROM
StudentsCTE
WHERE
RowNumber = 2
-- Simply select the number of row we want and that's it
GO