13 Replies Latest reply on Jan 17, 2019 3:11 PM by Luis Isoldi

    Execute this query in manual way

    Luis Isoldi

      Hello guys...

      You've been helping me a lot!

       

      I asked this before, but I guess I made the wrong question about it.

      Thing is, I need to make this work:

       

      SELECT

      DISTINCT ON (identities.user_id)

             users.id AS Id_usuario,

             users.name AS Nome_usuario,

             users.email AS Email_usuario,

             users.phone,

             users.birthday,

             users.cpf,

             users.street,

             users.state,

             users.street_number,

             users.created_at,

             users.source,

             users.last_sign_in_at,

             users.encrypted_password,

             (SELECT count(*) FROM pets WHERE pets.user_id = users.id) as pet_count

          FROM users

          INNER JOIN identities ON (identities.user_id = users.id)

          WHERE

            (identities.user_id is not null)

            AND users.id NOT IN 

              (

                SELECT users.id FROM users

                INNER JOIN roles on (roles.user_id = users.id)

                INNER JOIN businesses on (roles.resource_id = businesses.id)

                WHERE ((roles.resource_type = 'Business' OR roles.name = 'professional')));

       

      Without using CUSTOM QUERY.

       

      How can I join those tables to get the same result?