Quick report using Pivot and Grouping sets in SQL

Consider a scenario you want to create a quick report from a table product in SQL showing total product sold over the last 12 months, in which each column represents the month, last column could be the YTD sales. There will be the horizontal sub-total for each category for example sale by products, regions etc.

SQL Recursive Queries using CTEs

Have you frequently tried recursion in SQL? In Transact-SQL, we can use store procedure or function to perform a recursive call like other programing language. Another way is to rely on the Common Table Expression (or CTEs) to allow a query to reference itself in the SELECT statement.