简体   繁体   中英

PostgreSQL grouping error with Rails

I have the following query:

SELECT
EXTRACT (
    HOUR
    FROM
        interventions.created_at :: TIMESTAMP
) AS DATE,
COUNT (interventions. ID) AS total
FROM
    "interventions"
INNER JOIN medical_records ON (
    (
        medical_records.medical_recordable_type = 'Intervention'
    )
    AND (
        medical_records.medical_recordable_id = interventions. ID
    )
)
INNER JOIN patients ON (
    patients. ID = medical_records.patient_id
)
WHERE
    (
         interventions.created_at BETWEEN '2011-11-10 00:00:00'
         AND '2014-11-10 00:00:00'
    )
AND (
    medical_records.hospitalization = FALSE
)
AND (
    patients.birth_date BETWEEN '1892-12-31 23:50:39'
    AND '2013-12-31 22:59:59'
)
GROUP BY
    DATE
ORDER BY
    interventions. ID DESC,
    DATE ASC

And I get the following error:

PG::GroupingError: ERROR:  column "interventions.id" must appear in the GROUP BY clause or be used in an aggregate function

Here is the relevant part of my code :

query = query.select("EXTRACT(HOUR FROM interventions.created_at::timestamp) AS date, COUNT(interventions.id) AS total")
query = query.group("EXTRACT(HOUR FROM interventions.created_at::timestamp)")
query = query.order("EXTRACT(HOUR FROM interventions.created_at::timestamp) ASC")

I don't understand because the column "interventions.id" is used in the COUNT() function inside the select.

Any idea on how to resolve this issue?

Thank you.

从ORDER BY子句中删除interactions_id。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM