sql - Mysql Get data for Last Six weeks using JOIN -


i have edited query selecting employees data have done assessments in past 6 weeks. logically should each employee 2 time if has done assessments in 2 weeks query shows single record.

 select     assessmentemployee.    employeename,    avg(assessmentlisting.assessmentscore),    date_format((str_to_date(`assessmentsubmitteddatetime`, '%d-%b-%y %i:%i %p')) , '%y-%m-%v') _month   assessmentemployee   left join assessmentlisting   on  assessmentemployee.assessmentid=assessmentlisting.assessmentid   (str_to_date(`assessmentsubmitteddatetime`, '%d-%b-%y %i:%i %p') >= date_format(now() - interval 6 week, '%y' ))   group assessmentemployee.employeename  

i have following table using.

assessmentemployee  id  assessmentid  employeename 

other table assessmentlisting

id assessmentid assessmentsubmitteddatetime assessmentscore 

i want employees have score/ done assessments in last sex weeks , average score.

sample of data column of assessmentlisting

id  assessmentid     assessmentsubmitteddatetime     assessmentscore  1     040416024720     04-apr-2016 02:48 pm             50 

please try following query:

select     assessmentemployee.    employeename,    avg(assessmentlisting.assessmentscore),    date_format((str_to_date(`assessmentsubmitteddatetime`, '%d-%b-%y %i:%i %p')) , '%y-%v') year_week  assessmentemployee   left join assessmentlisting   on  assessmentemployee.assessmentid=assessmentlisting.assessmentid   unix_timestamp(date_format(str_to_date(`assessmentsubmitteddatetime`,'%d-%b-%y %i:%i %p'),'%y-%m-%d')) >= unix_timestamp(curdate() - interval 6 week)  group assessmentemployee.employeename, year_week; 

you shouldn't store date / time string. otherwise embrace these cumbersome jobs while processing them.


Popular posts from this blog

php - How should I create my API for mobile applications (Needs Authentication) -

5 Reasons to Blog Anonymously (and 5 Reasons Not To)

Google AdWords and AdSense - A Dynamic Small Business Marketing Duo