CREATE TABLE Person(
FirstName VARCHAR(10),
Age INT,
Gender CHAR(1))
INSERT INTO Person VALUES ('Ted',23,'M')
INSERT INTO Person VALUES ('John',40,'M')
INSERT INTO Person VALUES ('George',6,'M')
INSERT INTO Person VALUES ('Mary',11,'F')
INSERT INTO Person VALUES ('Sam',17,'M')
INSERT INTO Person VALUES ('Doris',6,'F')
INSERT INTO Person VALUES ('Frank',38,'M')
INSERT INTO Person VALUES ('Larry',5,'M')
INSERT INTO Person VALUES ('Sue',29,'F')
INSERT INTO Person VALUES ('Sherry',11,'F')
INSERT INTO Person VALUES ('Marty',23,'F')
SELECT *
FROM Person
/*
FirstName Age Gender
Ted 23 M
John 40 M
George 6 M
Mary 11 F
Sam 17 M
Doris 6 F
Frank 38 M
Larry 5 M
Sue 29 F
Sherry 11 F
Marty 23 F
*/
SELECT ROW_NUMBER() OVER ( ORDER BY Age ) AS [id] ,
FirstName ,
Age,
Gender
FROM Person
/*
id FirstName Age Gender
1 Larry 5 M
2 Doris 6 F
3 George 6 M
4 Mary 11 F
5 Sherry 11 F
6 Sam 17 M
7 Ted 23 M
8 Marty 23 F
9 Sue 29 F
10 Frank 38 M
11 John 40 M
*/
--不打乱原来的排序
SELECT ROW_NUMBER() OVER ( ORDER BY ( SELECT 0
) ) AS [id] ,
FirstName ,
Age,
Gender
FROM Person
/*
id FirstName Age Gender
1 Ted 23 M
2 John 40 M
3 George 6 M
4 Mary 11 F
5 Sam 17 M
6 Doris 6 F
7 Frank 38 M
8 Larry 5 M
9 Sue 29 F
10 Sherry 11 F
11 Marty 23 F
*/
SELECT RANK() OVER ( ORDER BY Age ) AS [Rank by Age] ,
FirstName ,
Age ,
Gender
FROM Person
/*
Rank by Age FirstName Age Gender
1 Larry 5 M
2 Doris 6 F
2 George 6 M
4 Mary 11 F
4 Sherry 11 F
6 Sam 17 M
7 Ted 23 M
7 Marty 23 F
9 Sue 29 F
10 Frank 38 M
11 John 40 M
*/
SELECT DENSE_RANK() OVER ( ORDER BY Age ) AS [Dense Rank by Age] ,
FirstName ,
Age ,
Gender
FROM Person
/*
Dense Rank by Age FirstName Age Gender
1 Larry 5 M
2 Doris 6 F
2 George 6 M
3 Mary 11 F
3 Sherry 11 F
4 Sam 17 M
5 Ted 23 M
5 Marty 23 F
6 Sue 29 F
7 Frank 38 M
8 John 40 M
*/
SELECT FirstName ,
Age ,
Gender ,
NTILE(2) OVER ( ORDER BY Age ) AS [Age Groups]
FROM Person
/*
FirstName Age Gender Age Groups
Larry 5 M 1
Doris 6 F 1
George 6 M 1
Mary 11 F 1
Sherry 11 F 1
Sam 17 M 1
Ted 23 M 2
Marty 23 F 2
Sue 29 F 2
Frank 38 M 2
John 40 M 2
*/
FirstName VARCHAR(10),
Age INT,
Gender CHAR(1))
INSERT INTO Person VALUES ('Ted',23,'M')
INSERT INTO Person VALUES ('John',40,'M')
INSERT INTO Person VALUES ('George',6,'M')
INSERT INTO Person VALUES ('Mary',11,'F')
INSERT INTO Person VALUES ('Sam',17,'M')
INSERT INTO Person VALUES ('Doris',6,'F')
INSERT INTO Person VALUES ('Frank',38,'M')
INSERT INTO Person VALUES ('Larry',5,'M')
INSERT INTO Person VALUES ('Sue',29,'F')
INSERT INTO Person VALUES ('Sherry',11,'F')
INSERT INTO Person VALUES ('Marty',23,'F')
SELECT *
FROM Person
/*
FirstName Age Gender
Ted 23 M
John 40 M
George 6 M
Mary 11 F
Sam 17 M
Doris 6 F
Frank 38 M
Larry 5 M
Sue 29 F
Sherry 11 F
Marty 23 F
*/
SELECT ROW_NUMBER() OVER ( ORDER BY Age ) AS [id] ,
FirstName ,
Age,
Gender
FROM Person
/*
id FirstName Age Gender
1 Larry 5 M
2 Doris 6 F
3 George 6 M
4 Mary 11 F
5 Sherry 11 F
6 Sam 17 M
7 Ted 23 M
8 Marty 23 F
9 Sue 29 F
10 Frank 38 M
11 John 40 M
*/
--不打乱原来的排序
SELECT ROW_NUMBER() OVER ( ORDER BY ( SELECT 0
) ) AS [id] ,
FirstName ,
Age,
Gender
FROM Person
/*
id FirstName Age Gender
1 Ted 23 M
2 John 40 M
3 George 6 M
4 Mary 11 F
5 Sam 17 M
6 Doris 6 F
7 Frank 38 M
8 Larry 5 M
9 Sue 29 F
10 Sherry 11 F
11 Marty 23 F
*/
SELECT RANK() OVER ( ORDER BY Age ) AS [Rank by Age] ,
FirstName ,
Age ,
Gender
FROM Person
/*
Rank by Age FirstName Age Gender
1 Larry 5 M
2 Doris 6 F
2 George 6 M
4 Mary 11 F
4 Sherry 11 F
6 Sam 17 M
7 Ted 23 M
7 Marty 23 F
9 Sue 29 F
10 Frank 38 M
11 John 40 M
*/
SELECT DENSE_RANK() OVER ( ORDER BY Age ) AS [Dense Rank by Age] ,
FirstName ,
Age ,
Gender
FROM Person
/*
Dense Rank by Age FirstName Age Gender
1 Larry 5 M
2 Doris 6 F
2 George 6 M
3 Mary 11 F
3 Sherry 11 F
4 Sam 17 M
5 Ted 23 M
5 Marty 23 F
6 Sue 29 F
7 Frank 38 M
8 John 40 M
*/
SELECT FirstName ,
Age ,
Gender ,
NTILE(2) OVER ( ORDER BY Age ) AS [Age Groups]
FROM Person
/*
FirstName Age Gender Age Groups
Larry 5 M 1
Doris 6 F 1
George 6 M 1
Mary 11 F 1
Sherry 11 F 1
Sam 17 M 1
Ted 23 M 2
Marty 23 F 2
Sue 29 F 2
Frank 38 M 2
John 40 M 2
*/