像Group By一样返回错误

时间:2014-05-05 02:06:13

标签: sql oracle sqlplus

似乎一旦我在查询中使用Group By,我就开始在where子句中返回错误。

SELect Rtrim(riders.firstName)||' '||rtrim(riders.lastname) AS Rider_Name, teams.teamname, Rtrim(agents.firstName)||' '||rtrim(agents.lastname) AS Agent_Name
FROM Riders, teams, agents, participation, races
Where teams.teamID = riders.teamID
and agents.agentID = riders.agentID
and participation.riderID = riders.riderid
and races.raceid = participation.raceid
and races.RaceDate Between '01-Apr-2008' and '30-Apr-2008'

返回我需要的结果,但我需要消除任何重复,所以我已经完成了

Group BY Rtrim(riders.firstName)||' '||rtrim(riders.lastname)

Group BY participation.riderID

Group By rider.riderID

这些是我最好的猜测。 这是完整的代码。

SQL> SELect Rtrim(riders.firstName)||' '||rtrim(riders.lastname) AS Rider_Name,
teams.teamname, Rtrim(agents.firstName)||' '||rtrim(agents.lastname) AS Agent_Na
me
  2  FROM Riders, teams, agents, participation, races
  3  Where teams.teamID = riders.teamID
  4  and agents.agentID = riders.agentID
  5  and participation.riderID = riders.riderid
  6  and races.raceid = participation.raceid
  7  and races.RaceDate Between '01-Apr-2008' and '30-Apr-2008'
  8  Group BY participation.riderid
  9  ;
SELect Rtrim(riders.firstName)||' '||rtrim(riders.lastname) AS Rider_Name, teams
.teamname, Rtrim(agents.firstName)||' '||rtrim(agents.lastname) AS Agent_Name
             *
ERROR at line 1:
ORA-00979: not a GROUP BY expression

2 个答案:

答案 0 :(得分:1)

如果您想要消除完全重复的行,您只需使用select distinct

SELect DISTINCT Rtrim(riders.firstName)||' '||rtrim(riders.lastname) AS Rider_Name,
       teams.teamname, Rtrim(agents.firstName)||' '||rtrim(agents.lastname) AS Agent_Name

然后不要使用group by

答案 1 :(得分:0)

我建议使用Group By rider.riderID

你只需要将rider.riderID添加到字段列表

FWIW group by比distinct

更快