Use SQL statements in Oracle for arrangement and combination

Source: Internet
Author: User

SQL> select * from users;
 
NAME value ID
-------------------
T1 A 1
T2 B 2
T3 C 3
T4 D 4

 

-- Implement the combination of values in the value column

 

SQL> select Replace (sys_connect_by_path (value, '#'), '#') combo
2 from users
3 where level = 2
4 connect by prior value <value and level <= 2;
 
Combo
--------------------------------------------------------------------------------
AB
AC
Ad
BC
BD
CD

6 rows selected

 

-- Arrange values in the value column in pairs

 

SQL> select Replace (sys_connect_by_path (value, '#'), '#') combo
2 from users
3 where level = 2
4 connect by nocycle prior value! = Value and level <= 2;
 
Combo
--------------------------------------------------------------------------------
AB
AC
Ad
Ba
BC
BD
CA
CB
CD
Da
DB
DC
 
12 rows selected

 

 

The test script is as follows:

Create Table users (name char (2), value char (1), ID number );
Insert into users values ('t1', 'A', 1 );
Insert into users values ('t2', 'B', 2 );
Insert into users values ('t3 ', 'C', 3 );
Insert into users values ('t4 ', 'D', 4 );
Commit;

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.