Working with Data Sources 6

本文介绍了SQL中GROUP BY函数的使用方法,包括如何按不同需求对表格进行分类汇总、如何更改查询结果中的列名、如何使用HAVING子句替代WHERE子句以及如何利用ROUND函数对查询结果进行四舍五入。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1. GROUP BY function

  The group by function will categroy the table according to different requirement:

  Select Employed, Major_category, SUM(Employed) FROM recent_grads GROUP BY Major_category; #Here, in the table, the program first group by Employed and Major_category. Then they use aggregate function to get the result.

2. If we would like to change name of each column in the query result, we can use AS function:

  Select sum(Men) as total_men, sum(Women) as total_women from recent_grads;

3. When we use AS, we choose HAVING instead of WHERE:

  Select Major_category, Avg(low_wage_jobs)/ Avg(Total) as share_low_wage from recent_grads group by Major_category having share_low_wage > 0.1;   

4. We use ROUND function to round values when query them:

  SELECT Major_category, ROUND(ShareWomen, 2) AS rounded_share_women 

  FROM recent_grads;  # Round the ShareWomen column to 2 decimal places

 

转载于:https://www.cnblogs.com/kingoscar/p/6079799.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值