SQL request, something's wrong with the setup.



  • Good evening! There is a request to remove clients who have visited 2 countries twice. He looks like this:

    SELECT client_id as "Клиент"
    FROM (
        SELECT client_id, country_id
        FROM zakaz
        GROUP BY client_id, country_id
        HAVING COUNT(*) > 1
    ) AS t1
    GROUP BY client_id
    HAVING COUNT(*) > 1
    

    In an attempt to frame instead id Fio, he's making a mistake. Here's what I'm doing:

    SELECT client.client_fio as "Клиент"
    FROM (
        SELECT client.client_fio, zakaz.country_id
        FROM client INNER JOIN client.client_id = zakaz.zakaz_id
        GROUP BY client.client_fio, zakaz.country_id
        HAVING COUNT(*) > 1
    ) AS t1
    GROUP BY client.client_fio
    HAVING COUNT(*) > 1
    

    Thank you for your help.



  • Corrected the request:

    SELECT t1.client_fio as "Клиент"
    FROM (
        SELECT client.client_fio, zakaz.country_id
            FROM client INNER JOIN client.client_id = zakaz.zakaz_id
            GROUP BY client.client_fio, zakaz.country_id
            HAVING COUNT(*) > 1
    ) AS t1
    GROUP BY t1.client_fio
    HAVING COUNT(*) > 1
    

    The mistake is, outside SELECT only knows about table t1 and knows nothing about the client.




Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2