-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRANK_data_analysis_function.sql
More file actions
36 lines (31 loc) · 964 Bytes
/
RANK_data_analysis_function.sql
File metadata and controls
36 lines (31 loc) · 964 Bytes
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
/*
RANKING: Returns a ranking value for each row
- If two or more rows tie for a rank, each receive the same rank
- If the first two rows tie for a ranking of 1, the next ranking value will be 3
- Rank does not always return consecutive integers
- Results can be divided into partitions
- Each Partition can have it's own ranking values
*/
--SELECT
-- BusinessEntityID
-- ,Rate
-- ,RANK() OVER (ORDER BY Rate DESC) AS RankByRate
--FROM HumanResources.EmployeePayHistory
--ORDER BY BusinessEntityID
--Rank within a partition
--NOTE that this query is partitioned on LocationID...
--Ranking values start over at 1 for every new LocationID
SELECT
i.ProductID
,p.Name
,i.LocationID
,i.Quantity
,RANK() OVER
(PARTITION BY i.LocationID
ORDER BY i.Quantity DESC
) AS 'Rank'
FROM Production.ProductInventory AS i
INNER JOIN Production.Product AS p
ON i.ProductID = p.ProductID
WHERE i.LocationID BETWEEN 3 AND 4
ORDER BY i.LocationID