Skip to content
Advertisement

SQL Windowing Ranks Functions

I am currently sorting products based on summation of Sales Amount in descending fashion and getting rank based on the summation of sales amount of every product in 2007 and ranking product 1 if it has the highest Sales Amount in that year and so forth.

My Database data's image

Currently my database table looks like the one mentioned in the image (apart from RankOf2008 and DRankOf2008 columns), I would like to have rankings in year 2008 for same top 5 products of 2007 (Null value if any of those top 5 products of 2007 are unsold in 2008) in the same table with side by side columns as shown in the image above.

Advertisement

Answer

May be you require something like this.

First getting ranks for all products then partition by year, that is rank of products year wise and fetching required data with help of CTE.

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement