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 6064281
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T09:15:47+00:00 2026-05-23T09:15:47+00:00

I am using Postgres and have the (working) SQL as something like: SELECT distinct(users.id),

  • 0

I am using Postgres and have the (working) SQL as something like:

SELECT 
distinct(users.id),
users.*
FROM 
public.addons, 
public.containers, 
public.users
WHERE 
( (addons."value" = 'Something' AND addons."name" = 'bob') OR
addons."value" = 'Something else' AND addons."name" = 'bill') AND 
(containers.id = addons.site_id AND 
users.id = containers.user_id)

What I want to do is format this so that it returns a set of user objects. I am not sure how I format this so that its something like (not working):

@users = User.find(:include => [:users, :containers, :addons], :conditions => {( (addons."value" = 'Something' AND addons."name" = 'bob') OR
    addons."value" = 'Something else' AND addons."name" = 'bill') AND 
    (containers.id = addons.site_id AND 
    users.id = containers.user_id) } )

Is this even possible?

  • 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-23T09:15:48+00:00Added an answer on May 23, 2026 at 9:15 am

    First, you should understand that DISTINCT is not a function, it’s a query modifier. The parentheses don’t change that. DISTINCT means if there are any rows where every column returned are identical, reduce the duplicates to a single row. You can’t reduce to a single row per distinct value in a single column (that’s the job of GROUP BY).

    Second, SQL result sets properly have one value per row per column. You could do some kind of aggregated string-concatenation like MySQL’s GROUP_CONCAT, or an equivalent aggregate function in PostgreSQL, but then you’ve got a string mashing together all the values, which you then have to explode in Ruby code after fetching it.

    So if you have multiple rows to fetch for a given users.id just let them come back to the app in multiple rows. Then do some post-processing of the data in your application. Loop over fetching the rows, and add the attributes to a Ruby object one by one. If you’re fetchiing data for multiple user id’s in a given query, stuff the attributes into a hash of your user objects, keyed by user id. That way you don’t need to fetch the rows in any particular order.

    Third, I’m inferring from your “name” and “value” columns that you’re using the Entity-Attribute-Value design, where users have multiple attributes, one per row. This is a design that subverts traditional relational data access, so you do end up having to write application code to preprocess and postprocess the data. If you could put each attribute into its own conventional column, which would be the relational way of storing data, your SQL queries would be a lot simpler.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a 32-bit Windows/Qt application using Postgres plugin. Recently, I've been intrigued to
I am porting an Oracle function into Postgres PGPLSQL.. I have been using this
I'm using a JMS queue to read from and insert data into a postgres
EDIT: I have been using Postgres with PostGIS for a few months now, and
I'm using Postgres, and I have a large number of rows that need to
I currently have to implement a query on a postgres database using a prepared
I'm using SA 0.6.6 , Python 2.66 and Postgres 8.3 . I have certain
I have a standard django setup using postgres, but I also want to access
I have a 9.0 postgres server instance and a database using UTF8 character encoding
I have this table in a postgres 8.4 database: CREATE TABLE public.dummy ( address_id

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.