Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5946103
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T16:46:31+00:00 2026-05-22T16:46:31+00:00

I’ve got a complicated query that I can’t wrap my head around (using either

  • 0

I’ve got a complicated query that I can’t wrap my head around (using either sql or ActiveRecord) Here are my models:

class Contact
  has_many :profile_answers
end

class ProfileAnswer
  belongs_to :contact
  belongs_to :profile_question
end

class ProfileQuestion
  has_many :profile_answers
end

I’m trying to find the number of ProfileAnswers for two contacts that have the same value for a particular ProfileQuestion. In other words:

Get the total number of profile answers that two contacts have answered with the same value for a particular profile_question

I don’t want to make multiple queries and filter as I know this is possible with Sql only, i just don’t know how to do it

I had considered a self join of profile_answers on profile_question_id then filtering by value being equal, but i still can’t wrap my head around that. Any help is greatly appreciated.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T16:46:32+00:00Added an answer on May 22, 2026 at 4:46 pm

    I think this will do:

    SELECT COUNT(DISTINCT profile_question_id)
    FROM 
      ( SELECT profile_question_id
        FROM ProfileAnswer an
          JOIN ProfileQuestion qu
            ON qu.id = an.profile_question_id
        WHERE contact_id IN ( id1, id2 )
        GROUP BY profile_question_id
               , value
        HAVING COUNT(*) = 2
      ) AS grp
    

    And the JOIN seems not be used. So, if ProfileAnswer.profile_question_id is NOT NULL, this will suffice:

    SELECT COUNT(*)
    FROM 
      ( SELECT profile_question_id
        FROM ProfileAnswer
        WHERE contact_id IN ( id1, id2 )
        GROUP BY profile_question_id
               , value
        HAVING COUNT(*) = 2
      ) AS grp
    

    EDITED for two specific contacts (with ids id1 and id2).

    Added the WHERE and changed the COUNT (DINSTINCT ) to COUNT(*).


    Perhaps this version with JOIN can be more easily adapted to ActiveRecord.

    Using JOIN

    SELECT COUNT(*)
    FROM ProfileAnswer a
      JOIN ProfileAnswer b
        ON a.profile_question_id = b.profile_question_id
        AND a.value = b.value
    WHERE a.contact_id = id1
      AND b.contact_id = id2 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.