Hierarchies are the bread and butter of most business applications and you find them almost everywhere:
* Product Categories
* Sales Territories
* Calendar and Time
Even when there is a big need from a business perspective, the solutions in relational databases are still sort of awkward. Since Version 2008 SQL Server makes live a bit easier with data type hierarchyid. To make use of aggregations you have to master GROUP BY clauses GROUPING SETS, CUBE, ROLLUP, WITH CUBE, or WITH ROLLUP as well. If you want to query of self-referenced tables you need a clear understanding of loops and common table expressions (CTE).
Join this session for a journey through best practices to model your hierarchies and handy scripts to transform your hierarchies into useful information. We will have fun playing around with a sample database based on G. R. R. Martin’s famous “Game of Thrones”.
10. Agenda
Aggregation, GROUP BY & OVER()
Several columns in one table
Several tables
Self-join in table
HierarchyID
11. Aggregations
Aggregate Functions
COUNT, SUM, MIN, MAX, AVG
GROUPING, GROUPING_ID
STDEV, STDEVP, VAR, VARP
COUNT_BIG, CHECKSUM_AGG
GROUP BY
ROLLUP
CUBE
GROUPING SET
OVER()
PARTITION BY
ORDER BY
17. GROUP BY ROLLUP 1/2
GROUP BY ROLLUP (
RegionName,
ContinentName,
WorldName
)
Region, Continent, World
Region, Continent
Region
()
18. GROUP BY ROLLUP 2/2
GROUP BY ROLLUP (
WorldName,
ContinentName,
RegionName
)
World, Continent, Region
World, Continent
World
()
19. GROUP BY CUBE 1/2
GROUP BY CUBE (
RegionName,
ContinentName,
WorldName
)
Region, Continent, World
Region, Continent
Region, World
Continent, World
Region
Continent
World
()
20. GROUP BY CUBE 2/2
GROUP BY CUBE (
WorldName,
ContinentName,
RegionName
)
Region, Continent, World
Region, Continent
Region, World
Continent, World
Region
Continent
World
()
21. GROUP BY GROUPING SETS
GROUP BY GROUPING SETS (
(RegionName, ContinentName,
WorldName),
(RegionName, ContinentName),
(RegionName, WorldName),
(ContinentName, WorldName),
(RegionName),
(ContinentName),
(WorldName),
()
)
Region, Continent, World
Region, Continent
Region, World
Continent, World
Region
Continent
World
()
22. How to implement Hierarchies in SQL Server?
Several columns in one table
Several tables
Self-join in table
HierarchyID
23. Several columns in one table
Eg. CityName, RegionName, ContinentName
Denormalized
Advantage
No joins needed
Disadvantage
Redundancy
Rather limited in the amount of groups/levels
25. Several tables
Eg. City.Name, Region.Name, Continent.Name
Normalized
References a row in another table
Advantage
No redundancy
Disadvantage
Joins needed
Rather limited in the amount of groups/levels
28. Self-join in table
Eg. Family.ID, Family.FatherID
References different row in the very same table
Advantage
Unlimited in the amount of levels
No redundancy
Disadvantage
Joins needed
Complex queries (Loops, recursive CTE)
31. Family Affairs
Child & Father
All descendants & all ascendants
X generations only
Grandfather & grandson
Ancestor & youngest generation
Uncles & aunts
Full family tree
32. I never bet against my family.
Tyrion Lannister (A Game of Thrones)
35. HierarchyID
Available since SQL Server 2008
Codes position within a hierarchy
Offers methods to access hierarchy attributes
Does not guarantee uniqueness
No foreign key constraints between nodes
36. Wrap up
Aggregation, GROUP BY & OVER()
Several columns in one table
Several tables
Self-join in table
HierarchyID
38. Resources
BOL: Using GROUP BY with ROLLUP, CUBE, and GROUPING
SETS
https://technet.microsoft.com/en-us/library/bb522495(v=sql.105).aspx
Itzik Ben-Gan (2015), T-SQL Querying, Microsoft Press
https://www.microsoftpressstore.com/store/t-sql-querying-
9780735685048
Steve Stedman (2015), Advanced CTE
http://stevestedman.com/2015/06/24-hours-of-pass-advanced-cte-
presenation/
40. Please review the event and sessions
EVENT SESSION
2016-09-
17 |
Footer Goes Here69 |
http://speakerscore.com/ZGVX
INSERT QR CODE
FROM WORD
DOCUMENT and
CHANGE THE URL AS
WELL
http://speakerscore.com/LT64