https://www.cnblogs.com/87060524test/p/10402784.html
在group中,group by 后面的字段,在select 后面是一定要有的
group by就是分组,把谁和谁分在一起,然后,在表里去找这个组合中满足的记录
select ACC_B2B..ORDER_Head.OrderSn from ACC_B2B..ORDER_Head group by ACC_B2B..ORDER_Head.OrderSn, ACC_B2B..ORDER_Head.CreateTime order by CreateTime desc
ACTION_TYPE,STATUS_CODE
在select聚合函数里,就可以不用在group里
select COUNT(*) ,ACTION_TYPE,STATUS_CODE,count(STATUS_MESSAGE) as STATUS_MESSAGE from ACC_MINIEHUB..hubV2_SOConfirmLog group by ACTION_TYPE ,STATUS_CODE
GROUP BY X意思是将所有具有相同X字段值的记录放到一个分组里,
GROUP BY X, Y意思是将所有具有相同X字段值和Y字段值的记录放到一个分组里。
HAVING
当然提到GROUP BY 我们就不得不提到HAVING,HAVING相当于条件筛选,但它与WHERE筛选不同,HAVING是对于GROUP BY对象进行筛选。
以下是引用片段: SELECT dep.department_name, MAX(emp.salary), MIN(emp.salary) FROM departments dep LEFT JOIN employees emp ON (dep.department_id = emp.department_id) GROUP BY dep.department_name --order by 的列,必须是出现在group by 子句里的列 ORDER BY dep.department_name DESC
这样可以看出,四个状态下的SoNumber是多少数量
select Status,COUNT(Number) from SOStatus group by SOStatus --having Status='3'