Mysql group function count usage

Source: Internet
Author: User
SELECTd. department, COUNT (CASEe. sexWHENe. sex & #39; f & #39; ANDp. position! & #39; supervisor & #39; THENp. idEND) fq, COUNT (CASEe. sexWHENe. sex & #39; f & #39; ANDp. position & #39; supervisor & #39; THENp. idEND) fz, COUNT (CASEe. sexWHENe. sex & #39; m & #39; ANDp. positi mysqlcount

SELECT d. department,
COUNT (CASE e. sex WHEN e. sex = 'f' AND p. position! = 'Supervisor 'THEN p. id END) fq,
COUNT (CASE e. sex WHEN e. sex = 'f' AND p. position = 'supervisor 'THEN p. id END) fz,
COUNT (CASE e. sex WHEN e. sex = 'm' AND p. position! = 'Supervisor 'Then p. id END) mq,
COUNT (CASE e. sex WHEN e. sex = 'm' AND p. position = 'supervisor 'THEN p. id END) mz,
COUNT (e. id) xj
FROM department d left join employ e ON d. id = e. deptid
Left join position p ON e. positionid = p. id
Group by d. id;

Why does the following two count statistics fail?
Why can we find the following?
SELECT d. department,
COUNT (case when e. sex = 'f' AND p. position! = 'Supervisor 'THEN p. id END) fq,
COUNT (case when e. sex = 'f' AND p. position = 'supervisor 'THEN p. id END) fz,
COUNT (case when e. sex = 'm' then case when p. position! = 'Supervisor 'THEN p. id END) mq
COUNT (case when e. sex = 'm' then case when p. position = 'supervisor 'Then p. id END) mz,
COUNT (e. id) xj
FROM department d left join employ e ON d. id = e. deptid
Left join position p ON e. positionid = p. id
Group by d. id

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.