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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T20:17:27+00:00 2026-06-13T20:17:27+00:00

On Mysql it works correctly. PG::Error: ERROR: for SELECT DISTINCT, ORDER BY expressions must

  • 0

On Mysql it works correctly.

PG::Error: ERROR: for SELECT DISTINCT, ORDER BY expressions must
appear in select list LINE 1: ) ORDER BY
programs.rating DESC, program_sc… ^ :

Query:

SELECT DISTINCT “programs”.* FROM “programs” INNER JOIN
“program_schedules” ON “program_schedules”.”program_id” =
“programs”.”id” WHERE (programs.rating >= 5 AND
program_schedules.start >= ‘2012-11-03 23:14:43.457659’) AND (ptype =
‘movie’) ORDER BY programs.rating DESC,
program_schedules.start DESC

Rails code:

@data =
Program.joins(:program_schedules).where(‘programs.rating >= ?
AND program_schedules.start >= ?’,5,
Time.now).order(‘programs.rating DESC,
program_schedules.start DESC’).uniq

I have tried with

Program.select(“programs.*,
program_schedules.*).joins(:program_schedules).where(…

but, in this way, when I’m going to read

@data.program_schedules

I get a nil value (When I know there are no nil values)

PostgreSQL 9.2 (Heroku), Ruby 1.9.2

Some info about my DB:

class Program < ActiveRecord::Base
   has_many :program_schedules
end


class ProgramSchedule < ActiveRecord::Base
  belongs_to :program
end

Schema.db

 create_table "program_schedules", :force => true do |t|
        t.integer  "program_id"
        t.datetime "start"
  end

  create_table "programs", :force => true do |t|
    t.string  "title",
    t.string  "ptype"
  end

EDIT:
I don’t need to order “programs_schedules” because I need all programs_schedules in my array related to that program.

  • 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-13T20:17:28+00:00Added an answer on June 13, 2026 at 8:17 pm

    You query is ambiguous in two ways:

    ptype is not table-qualified and you did not disclose the table definitions. So the query is ambiguous.

    More importantly, you want to:

    ORDER BY programs.rating DESC, program_schedules.start DESC
    

    At the same time, however, you instruct PostgreSQL to give you DISTINCT rows from programs. If there are multiple matching rows in program_schedules, how would Postgres know which one to pick for the ORDER BY clause? The first? Last? Earliest, latest, greenest? It’s just undefined.

    Generally, the ORDER BY clause cannot disagree with the DISTINCT clause, that’s what the error message tells you.

    Based on a few assumptions, filling in for missing information, your query could look like this:

    SELECT p.*
    FROM   programs p
    JOIN   program_schedules ps ON ps.program_id = p.id
    WHERE  p.rating >= 5
    AND    ps.start >= '2012-11-03 23:14:43.457659'
    AND    p. ptype = 'movie'   -- assuming ptype is from programs  (?)
    GROUP  BY p.id              -- assuming it's the primary key
    ORDER  BY p.rating DESC, min(ps.start) DESC;  -- assuming smallest start
    

    Also assuming you have PostgreSQL 9.0 or later, which is required for this to work. (Primary key covers whole table in GROUP BY.)

    As for:

    On Mysql it works correctly.

    No it doesn’t. It "works", but in mysterious ways rather than "correctly". MySQL allows for all sorts of weird mistakes and goes out of its way (and the SQL standard) to avoid having to throw exceptions – which is a very unfortunate way to deal with errors. It regularly comes back to haunt you later. Demo on Youtube.

    Question update

    I need all programs_schedules in my array related to that program.

    You might want to add:

    SELECT p.*, array_agg(ps.start ORDER BY ps.start)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this query which works correctly in MySQL. More background on it here
MySQL Query works fine using MySQL workbench but produces an error when I am
I was wondering how exactly inner joins works in mysql. If I do SELECT
Update 2022 There is cloud sql proxy now: https://cloud.google.com/sql/docs/mysql/sql-proxy#how-works Old Question I am currently
i'm trying to learn php/mysql. inserting data into mysql works fine but inserting those
I am confused how the following works in MySQL. In the queries below, the
I've the following condition in one of my model which works for mysql. with_scope
This MySQL script installs multiple triggers. It works on one machine running MySQL 5.0.51b-community.
I have a query which works fine in MySQL, but when I run it
I am trying to store a list of countries in a mySQL database. I

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.