Home > front end >  SELECT all from the table and sum the total for each given date
SELECT all from the table and sum the total for each given date

Time:10-03

Suppose there is a table StackOverflow

Stackoverflow

Date num_of_questions num_of_answers
2022/09/01 10 5
2022/09/01 20 5
2022/09/02 25 10
2022/09/02 10 9
2022/09/03 1 8
2022/09/03 8 2

Is there a way to select all entries within a given date range, and sum all the values for each same day? For instance, return the sum for the range 2022/09/01 - 2022/09/02: expectation return:

Date num_of_questions num_of_answers
2022/09/01 30 10
2022/09/02 35 19

So far I only figured out to write

SELECT * FROM Stackoverflow WHERE Date BETWEEN TIMESTAMP BETWEEN'2011/02/01' and '2011/02/02'; 

which return:

Date num_of_questions num_of_answers
2022/09/01 10 5
2022/09/01 20 5
2022/09/02 25 10
2022/09/02 10 9

(but I have not been able to sum this)

CodePudding user response:

SELECT Date, SUM(num_of_questions) as num_of_questions , SUM(num_of_answers) as num_of_answers
FROM Stackoverflow 
WHERE Date BETWEEN '2011/02/01' and '2011/02/02' GROUP BY Date;

Change your query as above then it will give the expected results.

  •  Tags:  
  • sql
  • Related