Search code examples
sqlms-accesscriteriams-access-2016

MS Access query required


I have data in a table "customer" like

ID    NAME    CITY
11    John     A
12    Peter    B
13    Robin    A
14    Steve    C
15    Methew   D
16    Matt     C
17    Nancy    C
18    Oliver   D

I want the query that only shows the data for every 2 customers that are in the same city.

Output should be,


ID    NAME    CITY
11    John     A
13    Robin    A
15    Methew   D
18    Oliver   D

Solution

  • The following query does this

    select a.ID1,a.Name1,a.City,b.cnt_of_customers
    from Customers as a
            ,(   SELECT City ,count(*) as cnt_of_customers
                  FROM Customers
               GROUP BY  City 
              HAVING count(*)=2) as b
    where a.city=b.city
    

    enter image description here