2 Replies Latest reply on Jan 10, 2019 4:55 AM by Luis Isoldi

    How do I execute this SQL in Tableau

    Luis Isoldi

      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 (businesses.imported = false

                  AND businesses.aasm_state = 10

                  AND (roles.resource_type = 'Business' OR roles.name = 'professional'))

               );