When the case is then else end statement to meet the sum or count and other statistical functions __ function

Source: Internet
Author: User

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,   

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.