我有两张桌子
CREATE TABLE j_work_status (
Work_req_id int(8) NOT NULL,
Work_status varchar(50) NOT NULL,
Date_Requested timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
date_assigned timestamp NULL DEFAULT NULL,
Date_completed timestamp NULL DEFAULT NULL,
Resource varchar(100) DEFAULT NULL,
Completed_by varchar(50) DEFAULT NULL,
PRIMARY KEY (Work_req_id),
CONSTRAINT j_work_status_ibfk_1 FOREIGN KEY (Work_req_id)
REFERENCES j_work_requests (Work_Req_ID)
) ENGINE=InnoDB DEFAULT CHARSET=utf8
和
CREATE TABLE j_survey_response (
Work_req_id int(8) NOT NULL,
Satisfaction_score tinyint(2) DEFAULT '0',
Response_score tinyint(2) DEFAULT '0',
Responder_score tinyint(2) DEFAULT '0',
Service_score tinyint(2) DEFAULT '0',
Score_avg decimal(3 , 2 ) DEFAULT '0.00',
Satisfaction_notes varchar(500) DEFAULT NULL,
PRIMARY KEY (Work_req_id),
CONSTRAINT j_survey_response_ibfk_1 FOREIGN KEY (Work_req_id)
REFERENCES j_work_requests (Work_Req_ID)
) ENGINE=InnoDB DEFAULT CHARSET=utf8
这两个表都有近20K的记录。 如果给出年份和季度,我需要显示其work_status完成的前3个季度的得分_avg的计数。
例如,如果年份= 2016年和季度= 1,我需要显示Q1-2016,Q4-2015,Q3-2015,Q2-2015的详细信息。
直到现在我有查询显示按年份和季度分组的所有详细信息。
select
work_status,
count(score_avg) survey_completed,
quarter(date_completed) Quarter,
year(date_completed) Year
from
j_work_status
join
j_survey_response ON j_survey_response.work_req_id = j_work_status.work_req_id
where
work_status = 'complete' and year(date_completed)
group by Quarter , Year
请有人帮助我找到之前的3季度细节