├── AdventureWorksDW2014.bak ├── README.md ├── Rank- Function ├── .gitkeep ├── Rank Function.sql └── Rank_output.xlsx ├── YoY Analysis ├── YearOverYear Analysis.sql ├── YoY_output (Recovered).xlsx └── gitkeep └── _config.yml /AdventureWorksDW2014.bak: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Mantej-Singh/SQL-Tips-Tricks-for-Data-Analysis/f5c017c52723cdc08a8c428d04bfe2814c0e4afe/AdventureWorksDW2014.bak -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # SQL-Hacks-for-Data-Analysis 2 | ##Simple sql server functions for data analysis 3 | 4 | * Download and restore the back up file `AdventureWorksDW2014.bak` from [Adventure Works 2014 Sample Databases](https://msftdbprodsamples.codeplex.com/releases/view/125550) 5 | 6 | 7 | # Outputs: 8 | ## YoY_output:- 9 | 10 | [![screenshot-1.png](https://i.postimg.cc/28LcSys6/screenshot-1.png)](https://postimg.cc/1fPcWm5h) 11 | 12 | ## Rank_output:- 13 | 14 | [![screenshot-1.png](https://i.postimg.cc/L6ZSFHvk/screenshot-1.png)](https://postimg.cc/6ywgdxkT) 15 | 16 | - - - - 17 | # When to use SQL Joins: 18 | [![scdreenshot_1.png](https://www.codeproject.com/KB/database/Visual_SQL_Joins/Visual_SQL_JOINS_V2.png) 19 | -------------------------------------------------------------------------------- /Rank- Function/.gitkeep: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /Rank- Function/Rank Function.sql: -------------------------------------------------------------------------------- 1 | use AdventureWorksDW2014; 2 | 3 | 4 | --Find the top products of 2013 5 | 6 | SELECT 7 | RANK() OVER (ORDER BY sum(s.SalesAmount) DESC) as 'Rank'--You can use ROW_NUMBER() also 8 | , count(DISTINCT s.SalesOrderNumber) as 'OrderCount' 9 | , sum(s.SalesAmount) as 'TotalSales' 10 | , cat.EnglishProductCategoryName as 'Category' 11 | , sub.EnglishProductSubcategoryName as 'SubCategory' 12 | FROM FactInternetSales s 13 | INNER JOIN DimProduct p ON s.ProductKey = p.ProductKey 14 | INNER JOIN DimProductSubcategory sub ON p.ProductSubcategoryKey = sub.ProductSubcategoryKey 15 | INNER JOIN DimProductCategory cat ON sub.ProductCategoryKey = cat.ProductCategoryKey 16 | -- filter on Year 2013 17 | WHERE YEAR(s.OrderDate) = 2013 18 | GROUP BY 19 | cat.EnglishProductCategoryName 20 | , sub.EnglishProductSubcategoryName 21 | 22 | ORDER BY 1;--order by RANK() 23 | -------------------------------------------------------------------------------- /Rank- Function/Rank_output.xlsx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Mantej-Singh/SQL-Tips-Tricks-for-Data-Analysis/f5c017c52723cdc08a8c428d04bfe2814c0e4afe/Rank- Function/Rank_output.xlsx -------------------------------------------------------------------------------- /YoY Analysis/YearOverYear Analysis.sql: -------------------------------------------------------------------------------- 1 | USE AdventureWorksDW2014; 2 | 3 | 4 | WITH mnthlysales (YearNum, MonthNum, Sales) 5 | AS--Used to get previous year 6 | ( 7 | SELECT d.CalendarYear, d.MonthNumberOfYear, SUM(s.SalesAmount) 8 | FROM DimDate d, FactInternetSales s 9 | WHERE d.DateKey=s.OrderDateKey --and d.CalendarYear=2014 10 | GROUP BY d.CalendarYear, d.MonthNumberOfYear 11 | --ORDER BY 1 DESc 12 | 13 | ) 14 | -- Get Current Year and join to CTE for previous year 15 | SELECT 16 | d.CalendarYear AS Year 17 | , d.MonthNumberOfYear AS Month 18 | , ms.Sales AS PreviousYearSales 19 | , SUM(s.SalesAmount) AS CurrentYearSales 20 | --get the percentage of difference. Percentage Profit = (profit/cost price)x 100% 21 | --new one is how much % of old one, eg: 100 is what percent of 50--> 100(new) is 200% of 50(old)=new*100/old 22 | --, (SUM(s.SalesAmount)*100)/ms.Sales AS PercentChange 23 | ,(SUM(s.SalesAmount) - ms.Sales) / SUM(s.SalesAmount) AS 'PercentGrowth' 24 | ,CASE WHEN SUM(s.SalesAmount) > ms.Sales THEN 'PROFIT' ELSE 'LOSS' END AS FinancialStatement 25 | --I am finding the diff bet current sales amt and previous sales amount and then dividing them by cyrrent sales amount. 26 | 27 | FROM DimDate d,FactInternetSales s,mnthlysales ms 28 | 29 | WHERE d.DateKey = s.OrderDateKey -- current year 30 | AND d.CalendarYear-1 = ms.YearNum --previous year 31 | AND d.MonthNumberOfYear = ms.MonthNum 32 | GROUP BY 33 | d.CalendarYear 34 | , d.MonthNumberOfYear 35 | , ms.Sales 36 | ORDER BY 37 | 1 DESC, 2 DESC 38 | 39 | ; 40 | -------------------------------------------------------------------------------- /YoY Analysis/YoY_output (Recovered).xlsx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Mantej-Singh/SQL-Tips-Tricks-for-Data-Analysis/f5c017c52723cdc08a8c428d04bfe2814c0e4afe/YoY Analysis/YoY_output (Recovered).xlsx -------------------------------------------------------------------------------- /YoY Analysis/gitkeep: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /_config.yml: -------------------------------------------------------------------------------- 1 | theme: jekyll-theme-merlot --------------------------------------------------------------------------------