Regexp_replace of Oracle SQL and two-dimensional matrix simulation of SQL statements

Source: Internet
Author: User

Share two SQL statements

1. regexp_replace supported by Oracle SQL

SQL> select regexp_replace ('fuck', '(.) (.)', '/123') from dual;
 
Regexp_replace ('fuck ','(.)(
------------------------------
Ufkcy UO

(Haha)

There are two letters in a pair.

//////////////////////////////////////// /////////////////////////////////////

SQL> select regexp_replace ('fuck', '(.) (.)', '/123') from dual;
 
Regexp_replace ('fuck ','(.)(
------------------------------
Ukyu

Removes all characters at the odd position, leaving only the characters at the even position. spaces are characters at the odd position.

 

2. Use SQL statements to simulate a two-dimensional matrix

Idea: first, use cross join to construct a full orders table, then sort and group

Used table: EMP table, which has at least three fields: deptno, job, Sal

Deptno: the job constitutes the joint primary key. The Sal value is determined by the deptno and job fields. The Sal value may be null. Requirement: When the Sal value is empty, 0 is displayed.

 

select. deptno, B. job,
(
case
when exists (select * from EMP where deptno =. deptno and job = B. job) Then
(
select sum (SAL)
from EMP
where deptno =. deptno and job = B. job
group by deptno, job
)
else 0
end
) sum_sal
from
(
select distinct deptno
from EMP
) A
cross join
(
select distinct job
from EMP
) B
group by. deptno, B. job
order by. deptno, B. job

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.