(transfer) the usage of GROUP BY in oracle

Reprinted from:

http://blog.csdn.net/basenet855x/article/details/6694150

 

 

question:

select item.itemnum,item.in1,item.in4,inventory.location from item,inventory

where item.itemnum=inventory.itemnum

and inventory.location = 'DYB'

and item.in1='D/MTD/MRM'

GROUP BY ITEM.ITEMNUM

The prompt error is NOT A GROUP BY EXPRESSION

Answer:

GROUP BY is a group query. Generally, GROUP BY is used in conjunction with aggregate functions. You can think about it.

You use GROUP BY to group by the field ITEM.ITEMNUM, then the content of other fields is different, and how to display it if it becomes one-to-many, such as the following

A B

1 abc

1 bcd

1 asdfg

select A,B from table group by A

What are the results of the investigation you said,

A B

    abc

1 bcd

    asdfg

How do the 3 bars on the right become one, so you need to use an aggregate function, such as

select A,count(B) 数量 from table group by A

The result of this is

A quantity

1 3

There is a principle of group by, that is, among all the columns after select, columns that do not use aggregate functions must appear after group by

Discussion: MySQL and Oracle's analysis of group by is different!

mysql:

SELECT s.SName, sc.COUNT(CID) c
FROM SC sc JOIN S s ON sc.SID = s. SID
GROUP BY s.SID
HAVING c = ( SELECT COUNT(*) FROM C )

oracle:

SELECT s.SID, s.SName, sc.COUNT(CID) c
FROM   SC sc JOIN S s ON sc.SID = s.SID
GROUP BY s.SID, s.SName
HAVING c = ( SELECT COUNT(*) FROM C )


大家可以看到区别了,mysql对group by子句的限制有所放宽,除了集合函数之外的被查询字段也可以不参与分组。相反oracle则是严格要求,所以感觉mysql似乎更灵活一些。

Guess you like

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