When the case when then else end statement encounters statistical functions such as sum or count

The thing is because of this - I need to group by 2 dimensions and find the total number of cases (count) grouped by these 2 dimensions, but also need to find the total by different conditions in these 2 dimensions , the sum of the totals obtained under these different conditions is the total without adding the conditions. for example:

Suppose there is a tablename table, the data structure is as follows:

   Field: id A B condition2

SELECT
  COUNT(1) cnt,
  A,
  B
FROM
  tablename
WHERE 1 = 1
  AND B LIKE '201602%'
GROUP BY A,
  B
ORDER BY A,
  B

 This is the total number of groups grouped by A and B under certain conditions from the tablename table. Assuming that the conditions of my B remain unchanged, I need to count the field according to the condition (assuming that the condition is a field of enumeration values, that is, its value is 1, 2, 3 three possibilities) to change the total number, then I need to write several sql respectively:

SELECT
  COUNT(1) cnt,
  A,
  B
FROM
  tablename
WHERE 1 = 1
  AND B LIKE '201602%'
  AND condition = '1'
GROUP BY A,
  B
ORDER BY A,
  B

 The count statistic condition here is AND condition = '1' and also AND condition = '2' and AND condition = '3', and then the count statistic values ​​under these different condition values ​​are cnt1 cnt2 cnt3 respectively. cnt1 + cnt2 + cnt3 = cnt (statistical value without condition filter).

The question is, how to display both cnt and cnt1-cnt3 in one record ?

Before, I first obtained the set of cnt, then looped each record, and obtained the corresponding cnt after adding the condition to the record according to the A, B and condition values ​​of each record, but later found that this is very inadvisable , because each record has to connect to the database to execute the sql query, which is inefficient. .

Later, it was found that in this case, case when then else end can work, but it is very magical that it can also be combined with the count function:

 

ELECT
  COUNT(1) cnt,
  COUNT(
    CASE
      WHEN condition = '1'
      THEN 1
      ELSE NULL
    END
  ) cnt1,
  COUNT(
    CASE
      WHEN condition = '2'
      THEN 1
      ELSE NULL
    END
  ) cnt2,
  COUNT(
    CASE
      WHEN condition = '3'
      THEN 1
      ELSE NULL
    END
  ) cnt3,
  A,
  B
FROM
  tablename
WHERE 1 = 1
  AND B LIKE '201602%'
GROUP BY A,
  B
ORDER BY A,
  B

 Here we need to pay attention to the usage of the case when then else end in the count function; another point to note is that count(null) gets 0, which means that it does not meet the current conditions, and it is else null. At this time, count(null ) is 0~~, that is, it is not counted under the current conditions, otherwise count(1) is counted! ! !

It's amazing! Check out the results:



 Careful a few more records, you will find that each record contains both cnt and cnt1-cnt3, and cnt = cnt1 + cnt2 + cnt3. This shows that the record obtained by our sql sentence is correct!

 

In addition, for the same reason, the sum function surrounds case when then else end. It should be noted that when the current condition is not met and the else branch is taken, it should be written like this, sum(case when condition='1' then val else 0 end), that is sum(0) is 0.

 

To summarize: sum(0) = 0 , count(null) = 0. When you encounter statistics in this situation, you can consider the case when then else end statement. For example, you need to " simultaneously " count the total number of people, the number of boys and the number of girls in one record ~~~

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=327079428&siteId=291194637