Use SQL to get full sorting

Source: Internet
Author: User
The table Elements stores 7 numbers. You need to obtain the full arrangement of these numbers. No createtableElements (iintnotnullprimarykey) GOinsertintoElementsvalues (1), (2), (3), (4), (5), (6), (7); WithElementsWithWeightas (selecti, power (2, (I-1) aswgtfromElements) selec

The table Elements stores 7 numbers. You need to obtain the full arrangement of these numbers. No create table Elements (I int not null primary key) GOinsert into Elementsvalues (1), (2), (3), (4), (5), (6 ), (7); With ElementsWithWeight as (select I, power (2, (I-1) as wgt from Elements) selec

The table Elements stores 7 numbers. You need to obtain the full arrangement of these numbers. <无>
create table Elements(    i int not null primary key)GOinsert into Elementsvalues (1),(2),(3),(4),(5),(6),(7);
With ElementsWithWeight as(    select        i,        power(2,(i-1)) as wgt    from        Elements)select    E1.i, E2.i, E3.i, E4.i, E5.i, E6.i, E7.ifrom    ElementsWithWeight as E1,    ElementsWithWeight as E2,    ElementsWithWeight as E3,    ElementsWithWeight as E4,    ElementsWithWeight as E5,    ElementsWithWeight as E6,    ElementsWithWeight as E7where    (E1.wgt + E2.wgt + E3.wgt + E4.wgt + E5.wgt + E6.wgt + E7.wgt) = 127
Create table users (name char (2), value char (1), id number); insert into users values ('A', 'A', 1 ); insert into users values ('B', 'B', 2); insert into users values ('C', 'C', 3 ); insert into users values ('dd', 'D', 4); commit;
Select o. p as "arrange" from (select replace (sys_connect_by_path (value, ','), ',') p from users connect by nocycle value! = Prior value) o where length (o. p) = 2;
Select o. p as "arrange" from (select replace (sys_connect_by_path (value, ','), ',') p from users connect by nocycle value! = Prior value) o where length (o. p) = 3;
Select o. p as "arrange" from (select replace (sys_connect_by_path (value, ','), ',') p from users connect by nocycle value! = Prior value) o where length (o. p) = 4;

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.