1.是否select字段需要全部出现?
Oracle中出现在select列表中的字段或者出现在order by后面的字段,如果不是包含在分组函数中,那么该字段必须同时在group by子句中出现。
Mysql没有此限制
例:
Mysql中,以下SQL语句是成立的,可见这里的Group by
SELECT p.PDTNO, p.PDTNAME, sum(a.QUANTITY) AS applySumCount
FROM PRODUCT p
JOIN APPLY a ON p.PDTNO = a.PDTNO
GROUP BY PDTNO
ORDER BY sum(a.QUANTITY) DESC;
Oracle中,这样的行为则会提示ORA-00979: 不是 GROUP BY 表达式
必须写成: GROUP BY p.PDTNO, p.PDTNAME
才可以
SELECT t1.pdtno, t1.pdtname, t1.applySumCount
FROM(
SELECT p.PDTNO, p.PDTNAME, sum(a.QUANTITY) AS applySumCount
FROM PRODUCT p
JOIN APPLY a ON p.PDTNO = a.PDTNO
GROUP BY p.PDTNO
)t1
ORDER BY t1.applySumCount DESC;
准确来说,这是SQL标准里面严格语法和宽松语法的区别
参考: 神奇的 SQL 之层级 → 为什么 GROUP BY 之后不能直接引用原表中的列
2.Mysql中group by可以使用别名,Oracle 中不可以
如下:
Mysql中:像这样在Group by中使用别名是可以的
SELECT p.PDTNO pdt, p.PDTNAME, sum(a.QUANTITY) AS applySumCount
FROM PRODUCT p
JOIN APPLY a ON p.PDTNO = a.PDTNO
GROUP BY pdt
ORDER BY sum(a.QUANTITY) DESC;
但是在ORacle中,使用别名会报错:ORA-00904: "PDN": 标识符无效
SELECT t1.pdtno, t1.pdtname, t1.applySumCount
FROM(
SELECT p.PDTNO pdt, p.PDTNAME pdn, sum(a.QUANTITY) AS applySumCount
FROM PRODUCT p
JOIN APPLY a ON p.PDTNO = a.PDTNO
GROUP BY pdt, pdn
)t1
ORDER BY t1.applySumCount DESC;
标签:group,PDTNO,sum,Mysql,t1,applySumCount,Oracle,GROUP,SELECT
From: https://www.cnblogs.com/trmbh12/p/18057186