我如何计算男性和女性的数量

时间:2015-03-16 02:06:37

标签: mysql count

我的查询如下:

select d.question, b.response, count(b.response)
from sl_flow a
INNER JOIN ul_attempt_responses b on a.question_id = b.question_id and b.type = 1
INNER JOIN us_attempts c on c.id = b.attempt_id
INNER JOIN ss_questions d on d.id = a.question_id
where a.status = 1
and a.ckm_question = 0
and b.response 
group by a.question_id, b.response
order by a.question_order asc

上面给出了我在数据库中存在的问题,这些问题是活跃的以及他们的回答和计数。

但是我需要一个查询,它会给我一些回答每个问题的男性和女性。因此,我有另一个查询,它给出了男性和女性的数量:

SELECT 
concat(a.response, 's') as gender,
count(a.response) as count
FROM
ul_attempt_responses a
    INNER JOIN us_attempts b ON a.attempt_id = b.id
WHERE
a.question_id = 6 and a.type = 0 AND trim(a.response) != ''
GROUP by a.response;

我不确定,怎么做。对于性别,question_id为6,a表上的类型必须为0(a表为ul_attempt_responses)。

这是我到目前为止所得到的。但是,似乎我得到的结果可能不一致:

SELECT
gender.question 
,coalesce(sum(case final.Response when 'male' then gender.total end),0) as 'Males'
,coalesce(sum(case final.Response when 'female' then gender.total end),0) as 'Females'
FROM
(SELECT
stats . *,
(CASE concat(stats.userid, stats.QuestionID, stats.type)
WHEN @curType THEN @curRow:=coalesce(@curRow, 0) + 1
ELSE @curROw:=1
AND @curType:=concat(stats.userid, stats.QuestionID, stats.type)
END) + 1 AS rank
FROM
(SELECT
d.question as Question,
a.user_id AS UserID,
c.question_id AS QuestionID,
c.type as Type,
c.response AS Response,
a.campaign_id as campaign_id
FROM
us_attempts a
INNER JOIN ul_attempt_responses c ON a.id = c.attempt_id
RIGHT OUTER JOIN ss_profile_questions d ON c.question_id = d.id AND c.type = 0
LEFT OUTER JOIN sl_profile_flow f ON c.question_id = f.profile_question_id
RIGHT OUTER JOIN us_users g ON g.id = a.user_id
WHERE
f.status = 1
ORDER BY a.user_id , c.question_id , c.type , a.id desc) stats) final
INNER JOIN
(select b.user_id, c.question as question, count(1) as total
from ul_attempt_responses a
INNER JOIN us_attempts b on a.attempt_id = b.id
INNER JOIN ss_questions c on a.question_id = c.id and a.type = 1
group by b.user_id, c.id) gender on final.UserID = gender.user_id
where
final.rank = 2 and final.QuestionID = 6 and final.campaign_id = 3
group by gender.question;

有没有办法可以减少上面的查询,还是有更好的优化方式?

1 个答案:

答案 0 :(得分:1)

您可以使用sum和case / if的组合来获取计数。鉴于您的完整表结构不明确,我假设您有一个表(或一个可以生成一组行的SQL),其中包含以下字段:

response_id
question_id
response

然后是一个SQL,例如

select question_id
   , response
   , sum(if(gender='M',1,0)) as males
   , sum(if(gender='F',1,0)) as females 
from (select q.question_id
   , q.response
   , g.response as gender from answers as q 
left join answers as g on q.response_id=g.response_id and g.question_id=6
where q.question_id!=6) as t 
group by question_id, response

会给你一个表格的结果

question_id,response,males,females
1,A,1,2
1,B,1,0
2,A,0,1
2,B,1,1
2,C,1,0

为了解释代码,子查询为每个响应生成一组行,并将问题与性别问题的响应进行映射。在主要的select语句中,if语句在适当的列中为特定的性别生成1,并将它们相加,可以为您提供有多少特定性别对该问题的响应。

修改 正如@Strawberry所建议的那样,较短的版本将是

select q.question_id
     , q.response_id
     , sum(g.response='M') as males
     , sum(g.response='F') as females 
  from answers as q 
  left 
  join test as g 
    on q.response_id = g.response_id 
   and g.question_id = 6 
 where q.question_id != 6 
 group 
    by q.question_id
     , q.response