Oracle row and column Devices

Source: Internet
Author: User

The project collects statistics on the "reagent" and "consumables" purchase amount and number of purchases by each research group. At that time, it queries and collects statistics on the "reagent" purchase amount and number of purchases, then, query and count the purchase amount and number of purchases of "consumables. This method is inefficient, and it repeatedly accesses the database. If Oracle provides row and column device functions, this problem can be well solved.

Oracle 11g provides piovt

Oracle 10 Gb provides decode

The logic of decode is as follows:

Decode (value, if1, then1, if2, then2, if3, then3,... else)

If value equals to if1, the decode result is then1; If value equals to if2, the result is then2; If value equals to if3, the result is then3. If value does not match any value, returns false. Class is the switch with C:

 Swtch (value ){  Case   "  If1  " : "  Then1  " ; Break ;  Case   "  If2  " : "  Then2  " ; Break  ;  Default : "  Other  " ; Break  ;} 

 

Since I use ORACLE 10 Gb, decode is used as an example:

 
1 Select Sum(Sal), DT. dname2FromEMP t3JoinDept dt4OnT. deptno=DT. deptno5Group ByDname

Display result:

8750 Accounting
10875 Research
9400 sales

After device:

Select   Sum (Decode (Dt. dname, '  Accounting  '  , Sal) accounting,  Sum (Decode (Dt. dname, '  Research  '  , Sal) Research,  Sum (Decode (Dt. dname, '  Sales  ' , Sal) Sales  From  EMP t  Join  Dept dt  On T. deptno = DT. deptno

Display result:

Accounting Research sales

8750 10875 9400

The modification statistics on reagent consumables are as follows:

 Select   Sum (Decode (producttype, '  Reagent  ' , OD. sumprice) sjprice,  Count (Decode (producttype, '  Reagent  '  , OD. sumprice) sjcount,  Sum (Decode (producttype, '  Consumables  '  , OD. sumprice) hcprice,  Count (Decode (producttype, '  Consumables  ' , OD. sumprice) hccount, Nd. Department  From  Order_info o  Join  Order_details OD  On O. orderid =  OD. orderid  Join  User_info u  On U. ID =  O. userid  Join  Neodepart nd On Nd. ID =  U. Department  Where   1   =   1   Group   By Nd. Department

 

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.