A Game of Hierarchies From GROUP BY to Recursive CTEs
A Game of Hierarchies
From GROUP BY to Recursive CTEs
Thanks you our PLATINUM sponsors
Thanks you our GOLD and SILVER sponsors
Welcome to SQL Saturday Denmark
Introduction
Markus Ehrenmüller-Jensen
Business Intelligence Architect
SQL Server 2005 - 2016
BI Developer
Database Developer
Database Admin
@MEhrenmueller
/markus-ehrenmueller
sqlederhose.wordpress.com
Every fool loves to hear that he’s important.
Tyrion Lannister (A Dance with Dragons)
Valar Morghulis
All men must die
Valar Dohaeris
All men must serve
Agenda
Aggregation, GROUP BY & OVER()
Several columns in one table
Several tables
Self-join in table
HierarchyID
Aggregations
Aggregate FunctionsCOUNT, SUM, MIN, MAX, AVG
GROUPING, GROUPING_ID
STDEV, STDEVP, VAR, VARP
COUNT_BIG, CHECKSUM_AGG
GROUP BYROLLUP
CUBE
GROUPING SET
OVER()PARTITION BY
ORDER BY
Demo-Query
Aggregations
Aggregations
SELECT
POIName,
RegionName,
ContinentName,
WorldName,
COUNT(*) asCountOfSomething
FROM
dbo.GoT_POI
GROUP BY
POIName,
RegionName,
ContinentName,
WorldName
ORDER BY
WorldName,
ContinentName,
RegionName,
POIName;
OVER()
OVER(PARTITION
BY WorldName)
OVER(PARTITION
BY RegionName)
OVER(PARTITION
BY POIName)
OVER(PARTITION
BY ContinentName)
GROUP BY ROLLUP 1/2
GROUP BY ROLLUP (
RegionName,
ContinentName,
WorldName
)
Region, Continent, World
Region, Continent
Region
()
GROUP BY ROLLUP 2/2
GROUP BY ROLLUP (
WorldName,
ContinentName,
RegionName
)
World, Continent, Region
World, Continent
World
()
GROUP BY CUBE 1/2
GROUP BY CUBE (
RegionName,
ContinentName,
WorldName
)
Region, Continent, World
Region, Continent
Region, World
Continent, World
Region
Continent
World
()
GROUP BY CUBE 2/2
GROUP BY CUBE (
WorldName,
ContinentName,
RegionName
)
Region, Continent, World
Region, Continent
Region, World
Continent, World
Region
Continent
World
()
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
()
How to implement Hierarchies in SQL Server?
Several columns in one table
Several tables
Self-join in table
HierarchyID
Several columns in one table
Eg. CityName, RegionName, ContinentName
Denormalized
Advantage
No joins needed
Disadvantage
Redundancy
Rather limited in the amount of groups/levels
Several columns in one table
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
Several tables
Querying several tables
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)
Self-join in table
Querying a self-join table: Basic
Family Affairs
Child & Father
All descendants & all ascendants
X generations only
Grandfather & grandson
Ancestor & youngest generation
Uncles & aunts
Full family tree
I never bet against my family.
Tyrion Lannister (A Game of Thrones)
Demo-Query
Querying a self-join table through
recursive CTE
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
Wrap up
Aggregation, GROUP BY & OVER()
Several columns in one table
Several tables
Self-join in table
HierarchyID
What John Snow knows
nothing (Source: Ygritte)
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/
Questions?
Markus Ehrenmüller-Jensen
Business Intelligence Architect
SQL Server 2005 - 2016
BI Developer
Database Developer
Database Admin
@MEhrenmueller
/markus-ehrenmueller
sqlederhose.wordpress.com
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