Advertisement
elena1234

ROW_NUMBER and CASE statement ( T-SQL)

Mar 2nd, 2022 (edited)
2,345
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
T-SQL 0.79 KB | None | 0 0
  1. SELECT Production.Product.[Name] AS ProductName, ListPrice,
  2. Production.ProductSubcategory.[Name] AS ProductSubcategory,
  3. Production.ProductCategory.[Name] AS ProductCategory,
  4. [Price Rank] = Row_Number() OVER(ORDER BY ListPrice DESC),
  5. [Category Price Rank] = Row_Number() OVER(PARTITION BY Production.ProductCategory.[Name] ORDER BY ListPrice DESC),
  6. [Top 5 Price In Category] =
  7. CASE
  8.         WHEN ROW_NUMBER() OVER(PARTITION BY Production.ProductCategory.[Name] ORDER BY ListPrice DESC) <= 5 THEN 'Yes'
  9.         ELSE 'No'
  10. END
  11. FROM Production.Product
  12. JOIN Production.ProductSubcategory ON
  13. Production.Product.ProductSubcategoryID = Production.ProductSubcategory.ProductSubcategoryID
  14. JOIN Production.ProductCategory ON
  15. Production.ProductSubcategory.ProductCategoryID = Production.ProductCategory.ProductCategoryID
  16.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement