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

  • Home
  • SEARCH
  • 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 8101671
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T23:02:08+00:00 2026-06-05T23:02:08+00:00

How to make this (PostgreSQL) using Rails syntax?: SELECT fld1, fld2 FROM ( SELECT

  • 0

How to make this (PostgreSQL) using Rails syntax?:

SELECT fld1, fld2
FROM
(
    SELECT fld1, fld2, count(*) as cnt FROM data WHERE fld2 not in('exclude1', 'exclude2') GROUP BY fld1, fld2 ORDER BY COUNT(*) DESC LIMIT 100
    UNION ALL
    SELECT fld1, fld2, count(*) as cnt FROM data WHERE fld2 in('exclude1', 'exclude2') GROUP BY fld1, fld2
) x
ORDER BY x.cnt DESC

I made following:

my_data = (Data.all(
    :select => sel_clause,
    :conditions => "data.fld2 not in %s" % [in_clause],
    :group => grp_clause,
    :order => 'count(*) desc',
    :limit => @max_rows) <<
    Data.all(
        :select => sel_clause,
        :conditions => "data.fld2 in %s" % [in_clause],
        :group => grp_clause).order('cnt desc')

The problem is that this << is not a classical “UNION ALL”, but joining of two arrays, and “order” can not be applied to the resulting array.

  • 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-06-05T23:02:11+00:00Added an answer on June 5, 2026 at 11:02 pm

    This is a place where I tend to use find_by_sql. The results will be returned as an array with columns requested encapsulated as attributes of the model from which you call this method.

    http://ar.rubyonrails.org/classes/ActiveRecord/Base.html

    ~Charles~

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

Sidebar

Related Questions

How to make this syntax works : SELECT * FROM uploads, audienceuploadassociation WHERE uploads.member_id
A postgresql 9.1 DB writes data which is sent automatically from a machine. This
I make this php program to access mysql data foreach ($arrayOfID as $ID) {
I'm using a postgresql install to house my data. My Post model has an
We are currently shifting from using PostgreSQL installed on remote CentOS boxes to using
Using clojure and clojure-sql with postgresql. I am trying to make a bootstrap function,
Hi I have the following query : Player.select(Players.*, (SELECT COUNT(*) FROM Results WHERE Results.player_id
To make this more clear, I'm going to put code samples: $file = fopen('filename.ext',
I make this program :: #include<stdio.h> char *raw_input(char *msg); main() { char *s; *s
I make this post because a script of pure javascript tooltips, no jquery and

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.