Same query with another where clause

I have a table like:

date        passed    failed   subject
2015-2-1      2          1        maths
2015-2-1      3          2        cs
2015-3-1      1          2        maths
2015-12-1     2          1        maths

I have a form for choosing a start and end date. for example: If I select startdate = 2015-2-1and enddate=2015-3-1 my output should be:

date        passed   failed   
2015-2-1     5         3
2015-3-1     1          2

the conclusion should contain the total number of passed and unsuccessful (regardless of the subjects) for the same months as shown above.

This is the query I used: startdate=2015-2-1andenddate=2015-3-1

SELECT SUM(passed) ,SUM(failed) FROM student_log WHERE
(DATE(`date`)='2015-2-1')
union
SELECT SUM(passed) ,SUM(failed) FROM student_log WHERE
(DATE(`date`)='2015-3-1')

It works great. But if my end date is December, I have to repeat my merge request until December ... Please suggest an alternative.

+4
source share
3 answers

What about:

SELECT SUM(passed), SUM(failed), DATE(`date`) 
FROM student_log 
WHERE DATE(`date`) >= '2015-2-1' AND DATE(`date`) <= '2015-3-1'
GROUP BY DATE(`date`);

This will allow you to find all the tests between two dates and give you an account from each day.

+1
source

For this

join union
SELECT SUM(`pass`) as total_pass, SUM(`fail`) as total_fail 
FROM pass_fail 
WHERE (`date` = '2015-2-1' or `date` = '2015-3-1' ) 
GROUP BY `date`

enter image description here

Group By

+1

this request:

Select date,sum (passed),sum(failed) from Results group by date 

returns me the following results:

date   passed   failed


2015-02-01  5       3

2015-03-01  1       2

2015-12-01  2       1

In other words: a group by clause will display monthly data as needed.

+1
source

Source: https://habr.com/ru/post/1599066/


All Articles