Use the lead analysis function to construct a function similar to the 9*9 multiplication rule

Source: Internet
Author: User
Another brother asked for help today. A table in the database has the following data: How does noname1a2b3c4d use an SQL statement to display the following results: abacadbcbdcd is the strength of analysis functions for this data structure? Let's do an experiment below: createtablet (nonumber, namevarchar (2); insertintotvalues (1, );

Another brother asked for help today. a table in the database has the following data: no name 1 a 2 B 3 c 4 d how to use an SQL statement to display the following results: AB ac ad bc bd cd is the strength of analytical functions for this constructed data. Let's test it: create table t (no number, name varchar (2 )); insert into t values (1, 'A ');

Another brother asked for help today. A table in the database has the following data:
No name
1
2 B
3 c
4 d
Use an SQL statement to display the following results:
AB
Ac
Ad
Bc
Bd

Cd

This kind of constructed data is the strength of the analysis function. Let's test it as follows:

Create table t (no number, name varchar (2 ));
Insert into t values (1, 'A ');
Insert into t values (2, 'B ');
Insert into t values (3, 'C ');
Insert into t values (4, 'd ');
Commit;

Implementation 1:
Select decode (h2, '','', h1 | h2) B,
Decode (h3, '','', h1 | h3) c,
Decode (h4, '','', h1 | h4) d
From (select name h1,
Lead (name, 1) over (order by name) h2,
Lead (name, 2) over (order by name) h3,
Lead (name, 3) over (order by name) h4
From t );

B C D
------------
AB ac ad
Bc bd
Cd

Implementation 2: The rows are converted relative to implementation 1.
With tt (
Select name h1,
Lead (name, 1) over (order by name) h2,
Lead (name, 2) over (order by name) h3,
Lead (name, 3) over (order by name) h4
From t
)
Select * from (select decode (h2, '','', h1 | h2) B from tt
Union
Select decode (h3, '','', h1 | h3) c from tt
Union
Select decode (h4, '','', h1 | h4) d from tt)
Where B is not null;

B
----
AB
Ac
Ad
Bc
Bd
Cd

Implementation 3: You do not need to analyze functions.

Select a. name | B. name from t a, t B where a. no <B. no;

Appendix, a SQL statement for 9*9 multiplication:

Select r1 | '*' | r1 | '=' | r1 * r1,
Decode (r2, '','', r2 | '*' | r1 | '=' | r2 * r1) B,
Decode (r3, '','', r3 | '*' | r1 | '=' | r3 * r1) C,
Decode (r4, '','', r4 | '*' | r1 | '=' | r4 * r1) D,
Decode (r5, '','', r5 | '*' | r1 | '=' | r5 * r1) E,
Decode (r6, '','', r6 | '*' | r1 | '=' | r6 * r1) F,
Decode (r7, '','', r7 | '*' | r1 | '=' | r7 * r1) G,
Decode (r8, '','', r8 | '*' | r1 | '=' | r8 * r1) H,
Decode (r9, '','', r9 | '*' | r1 | '=' | r9 * r1) I
From (select level r1,
Lag (level, 1) over (order by level) r2,
Lag (level, 2) over (order by level) r3,
Lag (level, 3) over (order by level) r4,
Lag (level, 4) over (order by level) r5,
Lag (level, 5) over (order by level) r6,
Lag (level, 6) over (order by level) r7,
Lag (level, 7) over (order by level) r8,
Lag (level, 8) over (order by level) r9
From dual
Connect by level <10 );

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.