Statistical query-two ways to implement count based on criteria-oracle__oracle

Source: Internet
Author: User
SQL statement:
Select B.departmentname, B.name, 
       count (case if b.businessstate= ' Notsubmit ' then 1  else null end) notsubmit,< C2/>count (case when b.businessstate= ' abandoned ' then 7  else null-end) abandoned,
        count (b.id) allcounts
From Business B GROUP by B.departmentname, B.name

To qualify the condition, count is counted by using the word when then else.

In addition, you can build a view and count by sum:

View:

Create or replace view View_business_statistics
as select b.ID, b.name as businessname,b.departmentname,b.sldate as S Ldate, b.applydate as Applydate,
         decode (b.businessstate, ' Notsubmit ', 1,0) as Notsubmitnum,
         decode ( B.businessstate, ' approving ', 1,0) as Approvingnum, from
business B;

SQL statements that are counted by sum:

Select B.departmentname, B.name, 
       sum (b.notsubmitnum) notsubmit, 
       sum (b.abandoned) abandoned,
       count (b.id ) allcounts
from Business B group by B.departmentname, B.name

Create a view use Oracle's own decode keyword (equivalent to case when), repeat the columns 0 and 1, and use sum to get the total count.

The idea of building a view is to increase flexibility.


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.