Mysql query SQL-related summary (case when two syntaxes and use aggregate functions)

case when two kinds of functions using the grammar and polymerization:
1.
the SELECT
the CASE
the WHEN a.`accountChannel` = 'the BSR' THEN 'XX Bank'
the WHEN a.`accountChannel` = 'BGZ' THEN 'XX Bank'
the WHEN a.`accountChannel `= 'JZ' THEN 'abundance pay'
the ELSE 'other'
the END types
the FROM
` A table `
the GROUP BY a.`accountChannel`;

2.
the SELECT
the CASE accountChannel
the WHEN 'the BSR' THEN 'XX Bank'
the WHEN 'BGZ' THEN 'XX Bank'
the WHEN 'JZ' THEN 'abundance pay'
the ELSE 'other'
the END types
FROM `A table`
the GROUP BY a.`accountChannel `;


3. For example, a student list, a student id, name, teacher id, source (app or pc) we have such a demand we want to know every teacher has several students, and these students come from inside the app has several there are a few from the pc.
This little trouble, look at the following several students each teacher must have the teacher group id then count (1), this simple but we have to know every teacher the following students from several pc app is no doubt a few end or use to count and then with the case when usage to determine


SELECT count(1) '数量',stu.tid,
COUNT(CASE WHEN ly = 'pc' then 1 END) pc,
COUNT(CASE WHEN ly = 'app' then 1 END) app
FROM stu GROUP BY tid

# Meaning that as long as ly = pc even then accumulate in line with a back that does not matter with 1 count, you can write any number but we have the habit of writing such as we are accustomed to 1 on the total number of statistics like count (1)

Guess you like

Origin www.cnblogs.com/windy1012/p/12057420.html