0

I have a table A with thoses columns : date, ip, created_by.

I would like to display date, ip and created by of rows where for one ip it exists different created_by.

Thank you

I explain more :

table answers:

date    ip    user_id   something_else    something_else
--------------------------------------------------------

table users:

id  name  siret tel email...
----------------------------

I would like the list of answers from answers table (date + ip + name_of_the_user + siret_of_the_user) where for the same ip I have different users.

thank you

1
  • 4
    It's not clear what you are asking. Explain, add details, examples, your code ... Commented Jul 6, 2015 at 7:46

1 Answer 1

1

IPs with different created_by:

SELECT ip
     , Count(DISTINCT created_by) As number_of_created_by
FROM   your_table
GROUP
    BY ip
HAVING Count(DISTINCT created_by) > 1

Alternative method:

SELECT ip
     , Min(created_by) As min_created_by
     , Max(created_by) As max_created_by
FROM   your_table
GROUP
    BY ip
HAVING Min(created_by) < Max(created_by)

Something like this should suffice for getting the details of these "rows":

SELECT *
FROM   your_table As outer_part_of_query
WHERE  EXISTS (
         SELECT ip
              , Count(DISTINCT created_by) As number_of_created_by
         FROM   your_table
         WHERE  ip = outer_part_of_query.ip
         GROUP
             BY ip
         HAVING Count(DISTINCT created_by) > 1
       )
3
  • 2
    This assumes that all combinations of (ip, created_by) are unique, which might not be the case.
    – Andriy M
    Commented Jul 6, 2015 at 9:50
  • it is not the case indeed. So it will not work
    – Marc
    Commented Jul 6, 2015 at 10:07
  • @Marc answer updated to match this extra requirement,
    – gvee
    Commented Jul 6, 2015 at 12:44

Not the answer you're looking for? Browse other questions tagged or ask your own question.