Created
July 28, 2017 08:20
-
-
Save Amit-Hora/a5c95a593bf3be21909074bfc4ce1066 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
SELECT a.CountryRegion, a.StateProvince, a.City, | |
CHOOSE (1 + GROUPING_ID(a.CountryRegion) + GROUPING_ID(a.StateProvince) + GROUPING_ID(a.City), | |
a.City + ' Subtotal', a.StateProvince + ' Subtotal', | |
a.CountryRegion + ' Subtotal', 'Total') AS Level, | |
SUM(soh.TotalDue) AS Revenue | |
FROM SalesLT.Address AS a | |
JOIN SalesLT.CustomerAddress AS ca | |
ON a.AddressID = ca.AddressID | |
JOIN SalesLT.Customer AS c | |
ON ca.CustomerID = c.CustomerID | |
JOIN SalesLT.SalesOrderHeader as soh | |
ON c.CustomerID = soh.CustomerID | |
GROUP BY ROLLUP(a.CountryRegion, a.StateProvince, a.City) | |
ORDER BY a.CountryRegion, a.StateProvince, a.City; | |
The CHOOSE method gives us back the text that matches the neumric value starting with 1, as | |
CountryRegion StateProvince City Level Revenue | |
NULL Null NULL Total 17898 --> The total comes in level as the in CHOOSE mehod with GROUPING_ID we are getting 4 | |
UNIted Kingdom Null NULL United Kingdom Total 1876 --> the name of region comes in level as we had the 3 in GROUPING_ID in CHOOSE method | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment