0

Why I just can't use these columns in select statement? Is it necessary to use such group function GROUP BY c.CLIENT_ID,c.FNAME, c.LNAME . How to resolve this problem?

 SELECT 
          c.CLIENT_ID, c.FNAME, c.LNAME, COUNT(*) 
            FROM 
              RACE r 
                JOIN VAGON v
                  ON r.RACE_ID = v.RACE_ID
                JOIN TICKET t
                  ON v.VAGON_ID = t.VAGON_ID
                JOIN CLIENT c
                  ON c.CLIENT_ID = t.CLIENT_ID
            WHERE r.DEPART_PLACE = 'Lviv' AND r.DESTINATION_PLACE = 'Kyiv';
            GROUP BY c.CLIENT_ID
            HAVING COUNT(*) >= 2

2 Answers2

0

You gave the answer yourself. Yes, you need that columns in your group by or use an aggregation function (like min or max on it.

Why?

You know the row is made unique by the Client_ID but the database doesn't. So you have to tell it to it in order to let it determine how the query is constructed and should be executed.

And you should remove the ; after 'Kyiv';

Patrick Hofman
  • 153,850
  • 22
  • 249
  • 325
0

All the columns you select should be added in the group by except the expressions. Why? Because the group by work in a unique way.