Convert one row to multiple rows in Oracle

Source: Internet
Author: User

/** The core solution is to generate a column from 1 to 10 as the "secondary column "**/Select level l FROM dual connect by level <= 10;[Problem]Two tables A B
Table:
Id pid
A1 1
A2 2
A3 3
Table B:
Pid pnumber
1 2
2 3
3 5

Generate such information based on the number of pnumbers 
Id list 
A1 A1-1
A1 A1-2
A2 A1-1
A2 A1-2
A2 A1-3
A3 A1-1
A3 A1-2
A3 A1-3
A3 A1-4
A3 A1-5/** create A test table T1 that the landlord said A table **/Create table t1 (ID VARCHAR2 (10 ),Pid VARCHAR2 (10));/** Create the test table T2, that is, the B Table mentioned by the author **/Create table t2 (Pid VARCHAR2 (10 ),Pnumber VARCHAR2 (10));/** Insert T1 data and T2 test data **/Insert into t1 VALUES ('a1', '1 ');Insert into t1 VALUES ('a2 ', '2 ');Insert into t1 VALUES ('a3 ', '3 ');Insert into t2 VALUES ('1', '2 ');Insert into t2 VALUES ('2', '3 ');Insert into t2 VALUES ('3', '5 ');
/** The core solution is to generate a column from 1 to 10 as the "secondary column "**/Select level l FROM dual connect by level <= 10;
/** Final SQL **/SELECT T1.ID, 'a1-'| T3.DZ AS LISTFROM T1, T2, (select level dz from dual connect by level <= 10) T3WHERE T1.PID = T2.PIDAND T3.DZ <= T2.PNUMBEROrder by 1, 2;

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.