0

I have table like this

id     date        score
1   2017-01-01  5
1   2017-01-02  6
2   2017-01-03  7
2   2017-01-04  5
5   2017-04-02  4
5   2017-04-03  2
6   2017-04-04  9
7   2017-04-05  10
1   2017-05-28  5
1   2017-05-29  3
2   2017-05-30  5
2   2017-05-31  3

I want to sum the score based on date range

condition 1
2017-01-01 - 2017-01-04
condition 2
2017-04-01 - 2017-04-30
condition 3
2017-05-01 - 2017-05-31

the goal im looking for is to generate table shown below

id  sum_condition_1 sum_condition_2  sum_condition_3
1   11                null              8
2   12                null              8
5   null               6               null
6   null               9               null
7   null               10              null
Madhur Bhaiya
  • 28,155
  • 10
  • 49
  • 57
muhnandap
  • 49
  • 2
  • 10
  • Do you have a master table of `id` separately ? What happens if a `id` value does not have any `score` in the given date "condition" ranges ? Will that `id` value still be shown in output, with all sums as `null`. – Madhur Bhaiya Jul 18 '19 at 12:47
  • Possible duplicate of [MySQL pivot table](https://stackoverflow.com/questions/7674786/mysql-pivot-table) – Raymond Nijland Jul 18 '19 at 12:58
  • Question is a duplicate.. Simply `SUM(CASE WHEN date BETWEEN '2017-01-01' AND '2017-01-04' THEN score ELSE NULL END) AS sum_condition_1` and change the dates for the other columns.. – Raymond Nijland Jul 18 '19 at 12:59

0 Answers0