I've got 2 tables, one with sales and one with companies:

Sales Table

```
Transaction_Id   Shop_id     Sale_date    Client_ID

```

```
92356            24234       11.09.2018    12356
92345            32121       11.09.2018    32121
94323            24321       11.09.2018    21231
94278            45321       11.09.2018    42123

```

Company table

```
Client_ID  Company_name 

```

```
12345      ABC 
13322      ABC  
32321      BCD
22221      BCD   

```

What I want to achieve is distinct count of Clients from each Company for each pair of shops(Clients who had at least 1 transaction in both of shops) :

```
Shop_Id_1     Shop_id_2   Company_name    Count(distinct Client_id)

```

```
12356         12345       ABC             31
12345         14278       ABC             23
14323         12345       BCD             32
14278         12345       BCD             43

```

I think that I have to use self join, but my queries even with filter for one week is killing DB, any thoughts on that? I'm using Microsoft SQL server 2012.

Thanks