Oracle complex queries are SQL

Source: Internet
Author: User

One, over () analysis functions

Group Check before: SELECT * from Test t where (select COUNT (*) from Test a where t.type=a.type and T.scope>a.scope) <2;

--rank ()/dense_rank () over (partition by ...)
SELECT * FROM (select T.*,rank () over (partition by T.type ORDER by T.scope) A from TEST T) a where a.a<3

--dense_rank () Hierarchical sequential sorting
Select T.*,dense_rank () over (partition by T.type ORDER by T.scope) A from test T
--rank () graded Jump sort
Select T.*,rank () over (partition by T.type ORDER by T.scope) A from test T

SELECT * FROM Test t where 2> (SELECT COUNT (*) from Test a where t.type=a.type and T.scope>a.scope)
Select t.* from Test T, (select A.type,max (a.scope) scopes from test a group by A.type) d where T.type=d.type and T.scope=d . Scope

--Cartesian product
SELECT * FROM Test t,test a
Select t.* from Test T, (select A.type,max (a.scope) maxscope,min (a.scope) minscope from test a group by a.type) d where T. Type=d.type and T.scope=d.scope


--
Select T.*,d.maxscope-t.scope Maxscope,t.scope-d.minscope minscope
From Test T,
(Select A.type, max (a.scope) maxscope, Min (a.scope) minscope
From TEST A
Group by A.type) d
where T.type = D.type

--min ()/max () over (partition by ...)
Select T.*,
NVL (Max (t.scope) over (partition by T.type), 0)-T.scope maxscope,
T.SCOPE-NVL (min (t.scope) over (partition by T.type), 0) Minscope
From Test T

--lead ()/lag () over (partition by ...)
Select T.*,lead (t.scope,1,0) over (partition by T.type ORDER by T.scope) a--the same group after one
From Test T
Select T.*,lag (t.scope,1,0) over (partition by T.type ORDER by T.scope) a--the same group as the previous
From Test T



Select T.*,
First_value (T.scope) over (partition by T.type) First_sal,
Last_value (T.scope) over (partition by T.type) Last_sal,
SUM (t.scope) over (partition by T.type) Sum_sal,
AVG (T.scope) over (partition by T.type) Avg_sal,
Count (T.scope) over (partition by T.type) Count_num,
Row_number () over (partition by T.type ORDER by T.scope) row_num
From Test T


--Note: The method with the ORDER BY clause must have an order by when using the method


Oracle complex queries are SQL

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.