Skip to content

Instantly share code, notes, and snippets.

@rashivkp
Last active November 1, 2015 16:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rashivkp/616bd6bc26ddf24dff7f to your computer and use it in GitHub Desktop.
Save rashivkp/616bd6bc26ddf24dff7f to your computer and use it in GitHub Desktop.
select distinct student, date, sum(if(ifnull(k1.hour, 0)>0, 1, 0)) as fornoon,
sum(if(ifnull(k2.hour, 0)>0, 1, 0)) as afternoon from t1
left join t1 k1 on k1.student=t1.student, k1.date=t1.date and hour between 1, 4
left join t1 k2 on k2.student=t1.student, k2.date=t1.date and hour between 5, 7
; another approach, not tested
SELECT student, date
count(CASE WHEN t1.hour between 1,4 THEN 1 END ) AS fornoon,
count(CASE WHEN t1.hour between 5,7 THEN 1 END ) AS afternoon
from t1
group by student, date
order by student, date
@shakirullahi
Copy link

thanks, let me try

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment