group by usage in Oracle


In the select statement, you can use the group by clause to divide the rows into smaller groups. Once the objects of the select operation after grouping become the data after each grouping, the aggregate information of each group is returned by using the grouping function.

Use the having clause to limit the returned result set. The group by clause can group query results and return row summary information. Oracle groups the query results according to the value of the expression specified in the group by clause.
In a query statement with a group by clause, the columns specified in the select list are either the columns specified in the group by clause or contain the clustering function select max(sal), job emp group by job; (note the max (sal), the job of the job does not have to appear, but it is meaningful) The select and group by, having clauses of the query statement are the only places where the clustering function appears, and the clustering function cannot be used in the where clause. select deptno,sum(sal) from emp where sal>1200 group by deptno having sum(sal)>8500 order by deptno; When the having clause is used in the gropu by clause, only the groups that satisfy the having condition are returned in the query result . In a sql statement can have where clause and having clause. Having is similar to the where clause, both are used to set qualification conditions. The function of the where clause is to remove the rows that do not meet the where condition before grouping the query results, that is, filter the data before grouping, and the condition cannot contain aggregate functions. Use where condition to display specific rows.

The role of the having clause is to filter the groups that meet the conditions, that is, to filter the data after grouping. The conditions often contain aggregate functions, and the having condition is used to display specific groups, or multiple grouping criteria can be used for grouping.

When using order by sorting, the order by clause is placed after the group by and the sorting criteria of the order by clause cannot appear in the columns outside the select query.
Query the number of employees of each position in each department

select deptno,job,count(*) from emp group by deptno,job

/********************** ********************************************

Remember this will do:

before using When group by, there is a rule that needs to be followed, that is, the fields that appear in the select list, if not in the group function, must appear in the group by clause. (The fields in select cannot appear alone, they must appear in the group statement or in the group function.)
The list is as follows:
id content idplace
1 2009-06-05 Shanghai
1 2009-07-05 Guangzhou
2 2009-07- 08 Beijing
3 2009-08-10

Beijing The result to be obtained
id content idplace
1 2009-06-05 Shanghai
2 2009-07-08 Beijing
3 2009-08-10 Beijing

每个id只取一条记录,如遇到相同id的记录,筛选依据是选content字段值小的
select
  *
from
  tb t
where
  not exists(select 1 from tb where id=t.id and content<t.content)

select * from tb t where not exists(select 1 from tb where id=t.id and content<t.content)

select
  *
from
  tb t
where
  content=(select top 1 content from tb where id=t.id order by content)

select * from tb t where content=(select min(content) from tb where id=t.id )

select
  *
from
  tb t
where
  content in  (select   top 1 content  from tb where id=t.id  order by  content )
Go to http://www.cnblogs.com/wanggd/p/3512214.html

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326818221&siteId=291194637