I have been off track for a while due to heavy office commitments, even if it had me away I was able to gain a good chunk of knowledge which I’m willing to share with you all. Today I’m gonna discuss the impressive partition function which saved my day in so many ways.
It is as simple as its name states, we are partitioning the query output by giving a set of condition. This can be useful for many needs like given below.
1. Identify and remove the duplicates precisely.
2. Unlike the conventional Group By syntax, this allows more customized grouping when retrieving data.
Since I work with school data all night long I came up with a code from what I do.
WITH HighestMark AS (
SELECT
[StudentID]
,[Year]
,[Semester]
,[FullName]
,[Grade]
,[Year]
,[Semester]
,[Subject]
,[Mark]
,row_number() OVER(PARTITION BY [StudentID],[Subject] ORDER BY [StudentID] DESC) AS [rn]
FROM [SemesterResults]
)
SELECT * FROM HighestMark WHERE [rn] = 1
As you can see in this case I’m using the partition for identifying the highest mark obtained for each subject by a student considering all the exams.
So in simple words, you can use your choice of columns in the select statement to partition and also you can use any column of your choice to order (no need for it to be used in the partition).
Lets
windup for the day folks, last but not least I wanna say what are you waiting
for give it a shot.
Comments
Post a Comment