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