Skip to content
Advertisement

SQL Query : Column1 – Stores_id, Column2 – Repo_id. How to select stores with more than 1 repo?

I would like to select only stores_id with more than one repo_id. In the case below, I would like as a result: Stores 1 and 4, as they have 2 repos_id related to them.

Example Table

| Stores_id |  Repo_id |
|-----------|----------|
| 1         |  1       |
| 1         |  2       |
| 2         |  2       |
| 3         |  1       |
| 4         |  1       |
| 4         |  2       |
| 5         |  1       |

Advertisement

Answer

If I understand you Question correct here would be a small example with having:

create @test table (
        stores_id int,
        repo_id int)

insert into @test
values (1,1), 
       (1,2),
       (2,2),
       (3,1), 
       (4,1),
       (4,2),
       (5,1)

select store_id
  from @test
 group by stores_id
 having count(repo_id) > 1
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement