SQL one-time query a field statistic results under different conditions

Source: Internet
Author: User

Refer to this article: 51764100, thank the original author

There are two tables, respectively, the "operator" and "documents", according to the different types of documents to classify the summary ( sales orders, sales return orders, the number of pens and amounts ), and displayed in the same table, do not want to be used as two times the method of query and merge, research, finally FIX:

D_employee table

D_bilndx table

The code is as follows:

SelectB.inputid asticket number, e.fullname. asopen a single member,IsNull( (Select Count(*)           fromD_bilndxwhereDraft=3  andBiltype= A  andD_bilndx.inputid=e.id),0) as 'sales Open a single pen number',        IsNull( (Select sum(D_bilndx.amount) fromD_bilndxwhereDraft=3  andBiltype= A  andD_bilndx.inputid=e.id),0) as 'Sales Billing Amount',        IsNull( (Select Count(*)           fromD_bilndxwhereDraft=3  andBiltype= -  andD_bilndx.inputid=e.id),0) as 'number of sales chargebacks',        IsNull( (Select sum(D_bilndx.amount) fromD_bilndxwhereDraft=3  andBiltype= -  andD_bilndx.inputid=e.id),0) as 'Sales Return amount',        Count(B.biltype) astotal number of open orders,sum(B.amount) asOpen a single amount fromD_bilndx asb Left JoinD_employee ase onB.inputid=e.idwhereB.draft=3  and(B.biltype= A orB.biltype= - )Group  byB.inputid, E.fullname, e.id

Get the perfect result:

SQL one-time query a field statistic results under different conditions

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.