The question that I would like to answer is: Present the average number of students who travel in a particular route per week.
What I have tried:
SELECT SUM(NO_OF_SEATS) "WEEKLY AVG"
FROM RESER;
This code shows me the average number of students only, but I want the average per week and I don't know how to get that.
I tried this but it's wrong:
SELECT SUM(NO_OF_SEATS) "WEEKLY AVG"
TO_CHAR(TRUNC(MIN(TRAVEL_DATE), 'WW') + 1, 'FORMAT')
FROM RESER;
Tom H
47.5k15 gold badges90 silver badges133 bronze badges
1 Answer 1
Try this:
SELECT AVG(NO_OF_SEATS) "WEEKLY AVERAGE", TRUNC(TRAVEL_DATE,'IW')
FROM RESER
GROUP BY TRUNC(TRAVEL_DATE,'IW');
'WW' = Assumes the first day starts on January 1st and will go in 7 day increments. Weeks will potentially start on a day that is not Monday.
'IW' = Will always start the week on a Monday.
answered Apr 8, 2016 at 20:24
Sign up to request clarification or add additional context in comments.
2 Comments
Dresden
I wouldn't mind having another correct answer under my belt as well. Thanks!
lang-sql
SUM()
=AVG()
...