ERROR: column must appear in the GROUP BY clause or be used in an aggregate function in PostgreSQL

Dung Do Tien Oct 29 2021 169

I'm a newbie in PostgreSQL. I have two tables are member and racinggameticket. I want to get all members was played the game. The relation of two tables is one - many.

Because one member can play the game many times. So I use group by command to help distinct records. See command below:

select rc.memberid, m.displayname from racinggameticket rc
inner join member m on m.id = rc.memberid
group by rc.memberid

But when running this command I got an error: column "m.displayname" must appear in the GROUP BY clause or be used in an aggregate function.

ERROR:  column "m.displayname" must appear in the GROUP BY clause or be used in an aggregate function
LINE 1: select rc.memberid, m.displayname from racinggameticket rc i...
                            ^
(execution time: 62 ms; total time: 422 ms)

Anyone can explain to me why this error is thrown and how to solve it?

Have 1 answer(s) found.
  • S

    Saurabh Shukla Oct 29 2021

    With GROUP BY command, any column appearing in SELECT command must appear in GOUP BY command.

    So you can fix your issue with the query below:

    select rc.memberid, m.displayname from racinggameticket rc
    inner join membership m on m.id = rc.memberid
    group by rc.memberid , m.displayname

    I hope this answer is helpful for you.

Leave An Answer
* NOTE: You need Login before leave an answer

* Type maximum 2000 characters.

* All comments have to wait approved before display.

* Please polite comment and respect questions and answers of others.

Popular Tips

X Close