For every journey want to sum them to work out the distance between.
SELECT Journey.Journey_No, Stages.Stage_ID, SUM(Stages.Distance_Between)
FROM Journey, Journey_Stages, Stages
WHERE Journey.Journey_No=Journey_Stages.Journey_No
AND Journey_Stages.Stage_ID=Stages.Stage_ID;
Tables are as followed:
CREATE TABLE Journey_Stages(Journey_No integer REFERENCES
Journey(Journey_No),Stage_ID integer REFERENCES Stages(Stage_ID));
CREATE TABLE Stages(Stage_ID integer PRIMARY KEY, Start_Station
integer REFERENCES Stations(Station_ID), End_Station integer
REFERENCES Stations(Station_ID));
CREATE TABLE Journey(Journey_No integer PRIMARY KEY, Train_No integer
REFERENCES Train(Train_No));
However I am getting the following error:
ORA-00937: not a single-group group function
00937. 00000 - "not a single-group group function"
*Cause:
*Action: Error at Line: 273 Column: 26
I am certain this is something simple to resolve.
3 Answers 3
If you want the distance per Journey, you need to group by
Journey:
SELECT Journey.Journey_No, SUM(Stages.Distance_Between)
FROM Journey, Journey_Stages, Stages
WHERE Journey.Journey_No=Journey_Stages.Journey_No
AND Journey_Stages.Stage_ID=Stages.Stage_ID
GROUP BY Journey.Journey_No;
Compare this to Option #2 that Rolando posted and you'll notice I removed one column each from the SELECT and GROUP BY clauses.
You should do one of two things:
OPTION #1: Keep only aggregate columns
SELECT SUM(Stages.Distance_Between)
FROM Journey, Journey_Stages, Stages
WHERE Journey.Journey_No=Journey_Stages.Journey_No
AND Journey_Stages.Stage_ID=Stages.Stage_ID;
OPTION #2: Add a GROUP BY
clause
SELECT Journey.Journey_No, Stages.Stage_ID, SUM(Stages.Distance_Between)
FROM Journey, Journey_Stages, Stages
WHERE Journey.Journey_No=Journey_Stages.Journey_No
AND Journey_Stages.Stage_ID=Stages.Stage_ID
GROUP BY Journey.Journey_No, Stages.Stage_ID;
-
Technically correct but this doesn't explain the functional difference between the two options. Option #1 will give you the TOTAL sum of "Stages.Distance_Between" across all Journeys. Option #1 will give you the sum of "Stages.Distance_Between" for all Stages within Journeys (which is probably semantically incorrect because a the distance is already recorded at the stage level, there is no need to sum it).Colin 't Hart– Colin 't Hart2013年04月23日 07:33:30 +00:00Commented Apr 23, 2013 at 7:33
Option #3 Use a window function:
SELECT Journey.Journey_No,
Stages.Stage_ID,
SUM(Stages.Distance_Between) over (partition by Journey.Journey_No) as dist_sum
FROM Journey
JOIN Journey_Stages ON Journey.Journey_No=Journey_Stages.Journey_No
JOIN Stages ON Journey_Stages.Stage_ID=Stages.Stage_ID;
But I think you probably want Rolando's Option #2
-
Syntactically correct, but why have Stage_ID in the select clause? And this will return as many rows as there are Journey_Stages.Colin 't Hart– Colin 't Hart2013年04月23日 07:37:37 +00:00Commented Apr 23, 2013 at 7:37
-
@Colin'tHart: it's not clear for me from the original question what David wants. So I copied all the columns from the original SELECT. I agree that it's very likely that this is not what he is after - your answer is probably the way to go.user1822– user18222013年04月23日 07:43:09 +00:00Commented Apr 23, 2013 at 7:43