Oracle merges multiple rows of records into one row

Source: Internet
Author: User

1. The actual demand situation is as follows:

650) this.width=650; "src=" Http://img.dnbcw.info/2012413/3941000.png "alt=" 3941000.png "/>

Description: In the above table, a student has many courses and scores, now want to have a SQL statement, the course name and results are combined to display, as follows:

650) this.width=650; "src=" Http://img.dnbcw.info/2012413/3941001.png "width=" "border=" 0 "/>


Description: Use a field to represent all of the students ' courses and accomplishments, including the full course name and test scores



2, problem analysis

To solve this problem can be used to create a function to merge these 2 fields, but this is too cumbersome, because it is the use of oracle10g, so you can use the function Wmsys.wm_concat () to achieve this function.

3. Solutions

Sql:

The first step is to write a view view_ly_sjdxpt_jwcj, merging the 2 fields

SQL code:

CREATEORREPLACEVIEWVIEW_LY_SJDXPT_JWCJ as

Select

XH hm,

kcmc| | ZSCJ as KC_CJ

from DCUSER.LY_XS_BZKS_CJB B;

Results:

650) this.width=650; "src=" http://img.dnbcw.info/2012413/3941002.png "border=" 0 "alt=" 3941002.png "/>


The second step is to write a view, using wmsys.wm_concat () to merge the KC_CJ field

SQL code:

Createorreplaceviewview_ly_sjdxpt_jwcj_kz asselect Hm,wmsys.wm_concat (KC_CJ) as KCCJ from VIEW_LY_SJDXPT_JWCJ groupby Hm


Results:

650) this.width=650; "src=" http://img.dnbcw.info/2012413/3941003.png "border=" 0 "alt=" 3941003.png "/>


The third step is to use it in the SQL you actually need:

SQL code:

SELECT DISTINCT

' Academic performance ' dxlb,

XH hm,

(Select Lxdh fromdcuser. LY_XS_BZKS_XSJBXX where xh=b.xh) SJHM,

To_char (sysdate, ' Yyyymmddhh24miss ') CRSJ,

xm| | ' Hello classmate, this semester your results are as follows: ' | | C.KCCJ as NR,

"BZ,

"' FSSJ

FROMDCUSER.LY_XS_BZKS_CJB b

Leftouterjoin Dcuser_kz. View_ly_sjdxpt_jwcj_kz C on B.xh=c.hm

Results:

650) this.width=650; "src=" Http://img.dnbcw.info/2012413/3941004.png "width=" "border=" 0 "alt=" 3941004.png "/>



This article is from the "Endless Learning" blog, please be sure to keep this source http://xuewujingya.blog.51cto.com/9677490/1585969

Oracle merges multiple rows of records into one row

Related Article

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.