0

The following query works in MySQL:

SELECT
  f.created_date as time_sec
  ,sum(f.value) as value
  , date_format( f.created_date , '%a') as metric
FROM ck_view_fills as f
GROUP BY date_format(f.created_date, '%a' )

I have migrated my database to PostgreSQL and I am now converting my queries to match. My naive conversion looks like this:

SELECT
  f.created_date as time_sec
  ,sum(f.value) as value
  , to_char( f.created_date , "D") as metric
FROM ck_view_fills as f
GROUP BY to_char( f.created_date , "D")

This query is not accepted and the error message produced by PostgreSQL is the following:

Error in query (7): ERROR: column "f.created_date" must appear in the GROUP BY clause or be used in an aggregate function LINE 2: f.created_date as time_sec

As far as I can tell f.created_date is indeed used in the group by clause. I have also seen examples using this very syntax. So what is the cause of this error and how do I get around it?

1 Answer 1

2

Postgres is correct. In fact, your query would fail with the most recent versions of MySQL as well -- using the default settings.

Just use an aggregation function:

SELECT MIN(f.created_date) as time_sec,
       SUM(f.value) as value
       TO_CHAR(f.created_date, 'D') as metric
FROM ck_view_fills f
GROUP BY to_char(f.created_date , 'D');

You should have used a similar construct in MySQL (regarding MIN() -- or MAX()).

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.