Encounter Oracle Row to column

Source: Internet
Author: User

Row to column should be the database is more common operation, in Oracle can use pivot, decode, can refer to the blog of the Melon:

http://blog.csdn.net/ch7543658/article/details/41146809

SELECT name,        Max (DECODE (course, ' Java ', Gread)) as Java,        Max (DECODE (course, ' C # ', Gread)) as C #,        Max (DECODE (Course, ' C ', Gread)) As C,        MAX (DECODE (Course, ' SQL ', Gread)) as Sqlfrom Tgroup by Name;name             JAVA         C #          C        SQL-------------- ------------------------------------dai         90tu         60

A Variant row-to-column requirement was encountered when actually writing the report:

The data column Pocket_id,serial_number about 50 rows of records:



Requires that Serial_number be written out in the pocket_id order of 8 data per line, i.e.



Compared to the example mentioned in the melon, because there is no suitable group field, it takes a little thought to turn the ladder table into a report and adds a secondary field to achieve the required functionality:

Select Max (A) as A,max (B) as B,max (C) as C,max (D) as D,max (E) as E,max (F) as F,max (G) as G,max (h) as H from (SELECT trunc ( (p.pocket_id-1)/8,0) as RM,       DECODE (mod (pocket_id,  8), 1, Serial_number) as A,       DECODE (mod (pocket_id,  8), 2, Serial_number) as B,       DECODE (mod (pocket_id,  8), 3, Serial_number) as C,       DECODE (mod (pocket_id,  8) , 4, Serial_number) as D,       DECODE (mod (pocket_id,  8), 5, Serial_number) as E,       DECODE (mod (pocket_id,  8) , 6, Serial_number) as F,       DECODE (mod (pocket_id,  8), 7, Serial_number) as G,       DECODE (mod (pocket_id,  8), 0, Serial_number) as H from   data table  WHERE 1 = 1 and   other conditions ORDER by pocket_id ASC) Mgroup by m.rm ORDER by RM Asc
Query Result:


Encounter Oracle Row to column

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.