How to explain the logic of the following SQL query?

Request the following:

with tbl as
(
select [Week], Client
from (values (1, 'a'),
(1, 'b'),
(1, 'c'),
(2, 'f'),
(2, 'a'),
(3, 'q'),
(3, 'y'),
(3, 'c')) as tbl ([Week], Client)
)
select tbl.[Week], count(distinct t1.Client)
from tbl
inner join tbl t1
on tbl.[Week] >= t1.[Week]
group by tbl.[Week]


Request generates unique values cumulatively.
But to understand the logic of its execution can not, especially with inner join (explain how does the inner join is not necessary).

Can anyone describe in detail what makes a request?
July 8th 19 at 11:13
2 answers
July 8th 19 at 11:15
Solution
In a nutshell, is the growth trend of unique clients for weeks. Google - "running total". In fact, by the first week jointsa record for the first week to the second - first and second, etc. according to the condition tbl.[Week] >= t1.[Week]
July 8th 19 at 11:17
Every client jointsa with customers current and previous weeks. So 1 week will be 9 clients! Then grouped by tbl.Week to get unique weeks. For each week, the aggregation of customers - the sum of unique clients. Due to the number of unique first gain adjusted and get 3 client instead of 9

Find more questions by tags SQL