group by

select org, leaseform, sum(fesitmatecapital)
  from (
        
        select a.fesitmatecapital, b.fname_l2 org, c.fname_l2 leaseform
          from t_afl_con_contract a
         inner join t_org_baseunit b on a.fbusinessorgid = b.fid
         inner join t_afl_bd_leaseform c on a.fleaseformid = c.fid)


 group by rollup(org, leaseform)
 order by org;
--------------------------------
select org, leaseform, sum(fesitmatecapital)
  from (
        
        select a.fesitmatecapital, b.fname_l2 org, c.fname_l2 leaseform
          from t_afl_con_contract a
         inner join t_org_baseunit b on a.fbusinessorgid = b.fid
         inner join t_afl_bd_leaseform c on a.fleaseformid = c.fid)


 group by cube(org, leaseform)
 order by org;
 -------------------
 
 select org, leaseform, sum(fesitmatecapital),grouping(org),grouping(leaseform)
  from (
        
        select a.fesitmatecapital, b.fname_l2 org, c.fname_l2 leaseform
          from t_afl_con_contract a
         inner join t_org_baseunit b on a.fbusinessorgid = b.fid
         inner join t_afl_bd_leaseform c on a.fleaseformid = c.fid)


 group by cube(org, leaseform)
 order by org;
 
 -----------------------------
 
 select org, leaseform, sum(fesitmatecapital)
  from (
        
        select a.fesitmatecapital, b.fname_l2 org, c.fname_l2 leaseform
          from t_afl_con_contract a
         inner join t_org_baseunit b on a.fbusinessorgid = b.fid
         inner join t_afl_bd_leaseform c on a.fleaseformid = c.fid)


 group by grouping sets(org, leaseform)
 order by org;
<think>我们正在讨论GROUP BY的使用方法。根据用户的问题,他之前询问了关于SUM(CASE WHEN ...)的用法,现在转向GROUP BY。根据引用[1][2][3],GROUP BY通常用于分组统计,并与聚合函数(如SUM, COUNT, AVG等)一起使用。同时,引用[2][3]提到GROUP BY ALL在SQL Server中已被弃用,而Access中不支持GROUP BY ALL,但支持多列分组。 因此,我将围绕以下结构组织回答: 1. GROUP BY的基本语法和用途 2. 与聚合函数的结合使用 3. 多列分组 4. HAVING子句的使用 5. 注意事项(如GROUP BY ALL的弃用) 6. 示例演示 同时,我会根据引用[4]提到可以通过字典或Series进行分组(虽然这是Python pandas中的用法,但这里用户问的是SQL),所以重点放在SQL上。 注意:用户引用的[4]是Python pandas的groupby,但用户当前问题明确是SQL的GROUP BY,所以主要参考引用[1][2][3]。</think>### GROUP BY 使用方法详解 #### 一、基本概念与语法 **GROUP BY** 用于将查询结果按指定列分组,通常与聚合函数(SUM, COUNT, AVG, MAX, MIN)配合使用[^1][^2]: ```sql SELECT 分组列, 聚合函数(列) FROM 表名 GROUP BY 分组列; ``` #### 二、核心功能解析 1. **单列分组** 统计各部门员工数量: ```sql SELECT department, COUNT(*) AS employee_count FROM employees GROUP BY department; ``` **结果示例**: | department | employee_count | |------------|----------------| | A | 15 | | B | 22 | | C | 8 | 2. **多列分组** 统计各部门+职级的平均薪资(Access/SQL通用写法): ```sql SELECT department, job_level, AVG(salary) AS avg_salary FROM employees GROUP BY department, job_level; -- 多列分组[^2][^3] ``` 3. **与聚合函数组合** ```sql SELECT department, SUM(salary) AS total_salary, AVG(salary) AS avg_salary, MAX(salary) AS max_salary FROM employees GROUP BY department; ``` #### 三、HAVING 子句的用法 用于对分组结果进行过滤(WHERE 不能用于聚合结果): ```sql SELECT department, AVG(salary) AS avg_salary FROM employees GROUP BY department HAVING AVG(salary) > 50000; -- 筛选平均薪资>5万的部门[^1] ``` #### 四、注意事项与技巧 1. **SELECT 列的限制** SELECT 中的非聚合列必须出现在 GROUP BY 中: ```sql -- 正确 SELECT department, job_title, SUM(salary) GROUP BY department, job_title -- 错误(job_title未在GROUP BY中) SELECT department, job_title, SUM(salary) GROUP BY department ``` 2. **GROUP BY ALL 的弃用** SQL Server 的 `GROUP BY ALL` 已被标记为弃用,应避免使用[^2][^3]: ```sql -- 不推荐(未来版本移除) SELECT department, SUM(salary) GROUP BY ALL department -- 替代方案 SELECT department, SUM(salary) GROUP BY department ``` 3. **NULL 值处理** GROUP BY 会将所有 NULL 值归为一组: ```sql SELECT department, COUNT(*) FROM employees -- NULL部门会被单独分组 GROUP BY department; ``` #### 五、完整示例 ```sql -- 统计2020年后入职的各部门员工数及平均薪资 SELECT department, COUNT(*) AS headcount, ROUND(AVG(salary), 2) AS avg_salary FROM employees WHERE hire_date >= '2020-01-01' GROUP BY department HAVING COUNT(*) > 5 ORDER BY avg_salary DESC; ``` #### 六、性能优化建议 1. **索引优化** 为分组列创建索引: ```sql CREATE INDEX idx_dept ON employees(department); ``` 2. **减少分组列数量** 不必要的分组列会增加计算开销 3. **先过滤再分组** 使用 WHERE 提前过滤数据: ```sql SELECT department, AVG(salary) FROM employees WHERE salary > 30000 -- 先过滤 GROUP BY department; ``` > 💡 **典型应用场景**:销售报表、用户行为分析、库存统计等需要分组汇总的场景[^1]。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值