It's because of this--I need to group by 2 dimensions, figure out the total number of groups grouped by these 2 dimensions, but at the same time it is also necessary to find the total number of different conditions under these 2 dimensions, the sum of the sum of the total number of each of these different conditions, and the sum of the sums that are not added to the conditions. Like what:
Suppose there is a tablename table, the data structure is as follows:
Field: ID A B condition2 [SQL] view plain copy SELECT COUNT (1) CNT, A, b from TableName WHERE 1 = 1 and b like ' 201602% ' GROUP by A, b
This is the total number of packets grouped by a,b from the TableName table, assuming that now my B is unchanged, the total number of changes will need to be counted according to the condition field (assuming that condition is a field of an enumeration value, that is, its value is 1,2,3 three possible). So I need to write a few separate sql: [SQL] view plain copy SELECT COUNT (1) CNT, A, B from tablename WHERE 1 = 1 and b like ' 201602% ' and condition = ' 1 ' GROUP by A, b
The count statistic condition here is the and condition = ' 1 ' and possibly the and condition = ' 2 ' and and condition = ' 3 ', and then the count statistics for these different condition values are cnt1 cnt2 Cnt3 is Cnt1 + cnt2 + cnt3 = CNT (without the statistical value of the condition filter condition).
The question is, how to show both CNT and Cnt1-cnt3 in a single record.
I was first to find the set of CNT, recycle each record, according to each record of a,b and condition value to get the record plus condition after the corresponding CNT, but later found that this is very undesirable, because each record has to connect the database to execute the SQL query, appear inefficient.
It turns out that in this situation, case then else can work, but amazingly, he can combine it with the Count function:
[SQL] View plain copy 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,