Oracle query: Group query, remove the first record in each group __oracle

Source: Internet
Author: User

Group By Type field, sort by code, and remove the first record from each group

Method One:

Select Type,min (code) from Group_info
Group By type;

Note: The columns following the select are included in the GROUP BY clause, or with aggregate functions, otherwise there will be a syntax error.

Method Two:

SELECT * FROM (
SELECT Z.type, Z.code, Row_number ()
Over (PARTITION by Z.type Order by Z.code) as code_id
From Group_info Z
)
WHERE code_id = 1;

Here the Over () is the Oracle analysis function


Refer to SQL Reference documentation:

Analytic functions compute a aggregate value based on a group of rows. They differ from aggregate functions in, They return multiple rows for each group.

Analyzing an aggregate function evaluates a value based on a set of rows. They are different from aggregate functions that return multiple rows per group.


Analytic functions are the last set of operations performed into a query except for the final ORDER BY clause. All joins and a WHERE, GROUP by, and has clauses are completed before the analytic functions. are processed. Therefore, analytic functions can appear only in the select list or ORDER BY clause.

Executes a query in the last set of operations of the parse function, except for the last ORDER BY clause. All connections and places, GROUP by and previous terms complete the analysis processing function. Therefore, the parse function can only appear in the select list or the ORDER BY clause.


Syntax structure:

 over
 (Analytic_clause)

The Analytic_clause structure consists of:

[Query_partition_clause]
[Order_by_clause [Windowing_clause]]

That is: function name ([parameter]) over ([partition clause] [SORT clause [slide window clause]])

Here partition by-boot partition clauses are similar to group by in a clustered function, and the sort clause can be viewed as an order by in a SELECT statement.

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.