我有两个查询给出两组结果,我想将一个查询的结果除以另一个。
这是我的第一个问题:
SELECT COUNT(*)
FROM survey_event_answers a JOIN survey_events e ON a.eventid = e.eventid
WHERE e.event_status = 'Closed' AND
e.survey_date BETWEEN '2013-05-01' AND '2014-01-31' AND
a.response_options IN (10,11) AND a.questionid = 7
GROUP BY MONTH(e.survey_date) DESC;
以下是此查询的结果:
279
443
664
743
785
1312
1085
915
231
这是我的另一个疑问:
SELECT COUNT(*),e.survey_date
FROM `survey_event_answers` a INNER JOIN survey_events e ON a.eventid = e.eventid
WHERE e.event_status='Closed' AND
e.survey_date BETWEEN '2013-05-01' AND '2014-01-31' AND
a.questionid=7
GROUP BY MONTH(e.survey_date) DESC
以下是查询结果:
351
539
826
926
984
1654
1378
1165
844
我希望第一行结果集的第一行除以第二行第一行。
请帮助我如何做到这一点。
由于
答案 0 :(得分:0)
这是你在寻找什么?
SELECT rd1.r1/rd2.r2 `result`
FROM (
SELECT COUNT(*) r1,MONTH(e.survey_date) d1
FROM `survey_event_answers` a
INNER JOIN survey_events e
ON (a.eventid = e.eventid)
WHERE e.event_status='Closed' AND e.survey_date BETWEEN '2013-05-01' AND '2014-01-31'
AND a.response_options IN (10,11) AND a.questionid=7
GROUP BY MONTH(e.survey_date) DESC ) rd1
JOIN (
SELECT COUNT(*) r2, MONTH(e.survey_date) d2
FROM `survey_event_answers` a
INNER JOIN survey_events e
ON (a.eventid = e.eventid)
WHERE e.event_status='Closed' AND e.survey_date BETWEEN '2013-05-01' AND '2014-01-31'
AND a.questionid=7
GROUP BY MONTH(e.survey_date) DESC ) rd2
ON rd1.d1=rd2.d2
答案 1 :(得分:0)
你可以这样做:
SELECT SUM(IF(a.response_options IN (10,11), 1, 0))/COUNT(*)
FROM survey_event_answers a
JOIN survey_events e
ON a.eventid = e.eventid
WHERE e.event_status = 'Closed'
AND e.survey_date BETWEEN '2013-05-01' AND '2014-01-31'
AND a.questionid = 7
GROUP BY MONTH(e.survey_date) DESC;
这可能不会很快(但它比执行两次查询并加入结果更快),但可能会做你想要的(答案10或11的比例)。您可以将MONTH(e.survey_date)
添加为第二列。