[Application of sql]-oracle grouping function Roullup

Source: Internet
Author: User

The first part explains:
Oracle's GROUP BY statement supports rollup and cube statements in addition to the most basic syntax. In the case of group by ROLLUP (A, B, c), the group by IS first performed on (A, B, c), then group by (A, B), then (a) group by, and finally the group by operation of the whole table.

Because the work to make a report, so want to run a report directly in the database out, take part of the company data collation as follows:

SELECTClp. ItemName asPorvince, CLC. ItemName asCity,SUM( Case  whenC2. Itemname=' Standard Products '  ThenC.businesssumELSE 0 END) asStandard Products,SUM( Case  whenC2. Itemname=' VIP products '  ThenC.businesssumELSE 0 END) asVIP Products,SUM(c.businesssum) asTotal fromBusiness_contract C Left JOINStore_info S onC.stores = S.sno Left JOINCode_library CLP onS.province = CLP. ItemNo andClp. Codeno =' Provincecodebyhand '   Left JOINCode_library CLC onS.city = CLC. ItemNo andClc. Codeno =' Citycodebyhand '   Left JOINBusiness_type T onC.businesstype=t.typeno Left JOINCode_library C2 onT.promotiontype = C2. ItemNo andC2. Codeno =' Promotiontype 'WHEREClp. Itemname=' Guangdong province 'ORClp. Itemname=' Hunan Province 'GROUP  byClp. ITEMNAME,CLC. ItemName

Query Result:

Here you need to add a row at the end of the summary row, here can use the automatic summary function Roullup, with the group by, the following wording:

GROUP BY ROLLUP(CLP.ITEMNAME,CLC.ITEMNAME)

The results are as follows:

Will find that the function will have a small total in Guangdong province and Hunan province, and finally all the total, if only to make a total, you can use grouping_id to beautify, the code is as follows:

GROUP BY ROLLUP(CLP.ITEMNAME,CLC.ITEMNAME)HAVING grouping_id(CLP.ITEMNAME,CLC.ITEMNAME)!=1

The results are as follows:

Here is a description of grouping_id:
Total is grouping_id=3
Subtotal grouping_id=1
The record is grouping_id=0.

Above

[Application of sql]-oracle grouping function Roullup

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.