SQL TUTORIAL - SELF JOINS Hierarchy Tables

preview_player
Показать описание
Another video brought to you by BeardedDev, bringing you tutorials on Business Intelligence, SQL Programming and Data Analysis.

This SQL Tutorial is all about hierarchy tables and self joins.

We start off by looking at hierarchies that we are all aware of such as Parent and Child.

The video then describes how we can create a hierarchy table in SQL Server including a Primary Key and a Foreign Key that references the Primary Key in the same table.

If you are new to working with constraints in SQL Server please check out these videos:

If you would like to follow along with this SQL Tutorial then this is the create table statement used:

CREATE TABLE dbo.Staff
(
StaffId INT IDENTITY(1, 1) NOT NULL
CONSTRAINT PK_Staff_StaffId PRIMARY KEY (StaffId)
, StaffTitle VARCHAR(50) NOT NULL
, ManagerId INT NULL
CONSTRAINT FK_Staff_ManagerId FOREIGN KEY (ManagerId) REFERENCES dbo.Staff (StaffId)
)

We then insert some dummy data in to the Hierarchy table and have a look at the results when executing a SELECT statement.

On to discussing Self Joins, and the difference between using an INNER JOIN and a LEFT OUTER JOIN when joining a table to itself although the same applies if you were joining two separate underlying tables together.

Then we apply a Self Join and return results, as the table and columns are the same we need to apply aliases.

SELECT
A.StaffTitle
, B.StaffTitle AS ReportsTo
FROM dbo.Staff AS A
LEFT OUTER JOIN dbo.Staff AS B
ON A.ManagerId = B.StaffId
Рекомендации по теме
Комментарии
Автор

Thanks @beardeddev for all your efforts and answering all the comments . Your content on windowing functions are very helpful and the best available on youtube. Thanks a ton.

dgvj
Автор

Hello! I've been watching several of your videos (very good ones by the way) looking for a way to write a query with a recursive CTE to roll up the costs of a BOM from the bottom up. Can you tell me somewhere to get information or are you interested in making a video about it? Thank you!

guillermovenuto
Автор

In India Chandigarh is a city that is shared capital of 2 states: Haryana and Punjab.

deepanshudhillon
Автор

if i have 2 or more null value in MnagerID

kleberoliveira
Автор

can you please make a video on Self join
with date example, Thanks

girishrbhat
Автор

Hi there,
This does help a lot. I am having a specific challenge where I'm trying to work around JIRA and confluence limitations. In Confluence we can create a report from JIRA using Table Transformer.
What I have is a table with all the issues from a JIRA Project and it has a hierarchy of EPIC >> TASK/STORY/SPIKE >> SUBTASK
I have a massive amount of trouble trying to export and work with a report that lists out the tasks in a logical order, i.e. everything just comes out in a random order depending on the issue "Key" which is the primary key for the table. The subtasks are linked to tasks, stories or spikes by parent id column and in turn the tasks, stories and spikes are linked to the epics by parent id.
So a table might look like this:
Issue key Summary Issue Type Status Assignee Parent
ISS-1 Task 1 Task To Do bob ISS-10
ISS-27 Task 2 Task Done alice ISS-10
ISS-32 Task 3 Task Done john ISS-10
ISS-33 Task 4 Task Done mark ISS-15
ISS-16 Task 5 Task To Do bob ISS-15
ISS-15 Epic 1 Epic To Do
ISS-24 Epic 2 Epic To Do
ISS-10 Epic 3 Epic To Do
ISS-37 Subtask 1 Subtask In Progress alice ISS-1
ISS-38 Subtask 2 Subtask To Do john ISS-1
ISS-39 Subtask 3 Subtask To Do mark ISS-27
ISS-40 Subtask 4 Subtask In Progress alice ISS-27
ISS-45 Subtask 5 Subtask In Progress john ISS-16
ISS-41 Subtask
ISS-66 Spike 1 Spike To Do alice ISS-24
ISS-67 Story 1 Story To Do john ISS-24
ISS-69 Subtask 7 Subtask To Do mark ISS-66
ISS-72 Subtask 8 Subtask To Do alice ISS-66
ISS-70 Subtask 9 Subtask To Do john ISS-66
ISS-71 Subtask 10 Subtask To Do mark ISS-67
ISS-74 Subtask 11 Subtask To Do alice ISS-67
ISS-73 Subtask 12 Subtask To Do john ISS-67

And I want the output to read:
Issue key Issue
ISS-15 Epic Epic 1 To Do
ISS-33 Task Task 4 Done mark ISS-15
ISS-16 Task Task 5 To Do bob ISS-15
ISS-45 Subtask Subtask 5 In Progress john ISS-16
ISS-41 Subtask Subtask 6 Done mark ISS-16
ISS-24 Epic Epic 2 To Do
ISS-66 Spike Spike 1 To Do alice ISS-24
ISS-69 Subtask Subtask 7 To Do mark ISS-66
ISS-72 Subtask Subtask 8 To Do alice ISS-66
ISS-70 Subtask Subtask 9 To Do john ISS-66
ISS-67 Story Story 1 To Do john ISS-24
ISS-71 Subtask Subtask 10 To Do mark ISS-67
ISS-74 Subtask Subtask 11 To Do alice ISS-67
ISS-73 Subtask Subtask 12 To Do john ISS-67
ISS-10 Epic Epic 3 To Do
ISS-1 Task Task 1 To Do bob ISS-10
ISS-37 Subtask Subtask 1 In Progress alice ISS-1
ISS-38 Subtask Subtask 2 To Do john ISS-1
ISS-27 Task Task 2 Done alice ISS-10
ISS-39 Subtask Subtask 3 To Do mark ISS-27
ISS-40 Subtask Subtask 4 In Progress alice ISS-27
ISS-32 Task Task 3 Done john ISS-10


I've tried a few self joins but just can't seem to get the logic working.

loki
join shbcf.ru