Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

sql query with group by works on SQLite and not on postgresql

The following query works on local but not on production: (Production is heroku is running postgreSQL and locally I am running a sqllite database)

Ruby

Tutor.joins(:expertises).where(:expertises => {:subject_id => [2,4]}).group("tutors.id").having("COUNT(*) = 2")

SQL

SELECT "tutors".* FROM "tutors" INNER JOIN "expertises" ON "expertises"."tutor_id" = "tutors"."id" WHERE ("expertises"."subject_id" IN (9)) GROUP BY tutors.id HAVING COUNT(*) = 1 ORDER BY rank DESC)

I get the following error on production ActiveRecord::StatementInvalid (PGError: ERROR: column "tutors.fname" must appear in the GROUP BY clause or be used in an aggregate function

I have the following values in my table

id                 :integer         not null, primary key
fname              :string(255)
lname              :string(255)
school             :string(255)
major              :string(255)
year               :string(255)
bio                :text
vid                :string(255)
price              :integer
phone              :string(255)
skype              :string(255)
address            :text

When I try to adjust the query to group by all attributes, I get another error:

Ruby

>> Tutor.joins(:expertises).where(:expertises => {:subject_id => [2,4]}).group("tutors.*").having("COUNT(*) = 2")

SQL

SELECT "tutors".* FROM "tutors" INNER JOIN "expertises" ON "expertises"."tutor_id" =    "tutors"."id" WHERE ("expertises"."subject_id" IN (2, 4)) GROUP BY tutors.* HAVING COUNT(*)    = 2 ORDER BY rank DESC

ActiveRecord::StatementInvalid: PGError: ERROR: could not identify an ordering operator for type tutors HINT: Use an explicit ordering operator or modify the query.

Help!

like image 460
user531065 Avatar asked May 14 '11 23:05

user531065


People also ask

How is PostgreSQL different from SQLite?

SQLite is an Embedded DBMS. This means that it is a Serverless DBMS with the ability to run within your applications. PostgreSQL DBMS implements a Client-Server Model and requires a Database Server to set up and run over a network.

How does group by work in PostgreSQL?

The PostgreSQL GROUP BY clause is used in collaboration with the SELECT statement to group together those rows in a table that have identical data. This is done to eliminate redundancy in the output and/or compute aggregates that apply to these groups.

Can SQLite connect to PostgreSQL?

Steps for Connecting SQLite to PostgreSQLStep 1: Create SQLite DB Dumpdata Backup. Step 2: Generate a Postgres DB and User. Step 3: Configure Settings.py. Step 4: Import Required Fixture via Loaddata from SQLite to PostgreSQL.

What does group by do SQLite?

The SQLite GROUP BY clause is used in a SELECT statement to collect data across multiple records and group the results by one or more columns.


1 Answers

PostgreSQL does not support unaggregated and ungrouped expressions in GROUP BY queries.

Use this:

SELECT  t.*
FROM    (
        SELECT  tutor_id
        FROM    expertises e
        WHERE   e.subject_id IN (2, 4)
        GROUP BY
                tutor_id
        HAVING  COUNT(*) = 2
        ) e
JOIN    tutor t
ON      t.id = e.tutor_id

This is cross-platform.

like image 193
Quassnoi Avatar answered Sep 20 '22 08:09

Quassnoi