当前位置:网站首页>SQL Server AVG function rounding
SQL Server AVG function rounding
2022-06-24 10:09:00 【Cpsu】
SQL Sever The mean value function of is used to find whether the mean value of integer type data is the result or integer , And it doesn't round . That is, rounding down . Here, first create a grade sheet , And add data as appropriate .
CREATE TABLE sic(
Student number nchar(10) not null,
Course no. nchar(10) not null constraint pk_lh primary key( Student number , Course no. ),
achievement int not null);
INSERT INTO sic
values ('01','01',80),
('01','02',90),
('01','03',99),
('02','02',60),
('02','03',80),
('03','01',80),
('03','02',80),
('03','03',80);

SELECT AVG( achievement ) AS Divide equally
FROM sic
GROUP BY sic. Student number

Obviously, the student ID is 01 The average score of students should not be an integer . Here are two ways to solve .
The first is to set the data to... When creating the table float type .
ALTER TABLE sic
ALTER COLUMN achievement FLOAT NOT NULL;
SELECT sic. Student number ,AVG( achievement ) AS Divide equally
FROM sic
GROUP BY sic. Student number ;
# You can also use DECIMAL(18,2) Format retains two decimal places

The second way is to use SQL Server Of CAST perhaps CONVERT function .
SELECT sic. Student number ,
AVG(
CAST ( achievement AS FLOAT)
) AS Divide equally
FROM sic
GROUP BY sic. Student number
SELECT CAST(AVG(CONVERT(FLOAT, achievement )) AS DECIMAL(18,2)) AS Divide equally
FROM sic
GROUP BY sic. Student number
边栏推荐
- 2022-06-23:给定一个非负数组,任意选择数字,使累加和最大且为7的倍数,返回最大累加和。 n比较大,10的5次方。 来自美团。3.26笔试。
- oracle池式连接请求超时问题排查步骤
- 物联网?快来看 Arduino 上云啦
- JS proxy mode
- PHP encapsulates a file upload class (supports single file and multiple file uploads)
- 机器学习——感知机及K近邻
- Symbol.iterator 迭代器
- el-table点击添加行样式
- 大中型企业如何构建自己的监控体系
- Dragging El table sortablejs
猜你喜欢
随机推荐
SQL-统计连续N天登陆的用户
卷妹带你学jdbc---2天冲刺Day1
有关二叉树 的基本操作
微信小程序学习之 实现列表渲染和条件渲染.
port 22: Connection refused
小程序 rich-text中图片点击放大与自适应大小问题
The great charm of cookies
canvas无限扫描js特效代码
How to manage massive network infrastructure?
Go language development environment setup +goland configuration under the latest Windows
JS singleton mode
SQL Sever关于like操作符(包括字段数据自动填充空格问题)
Operator details
Open Oracle server under Linux to allow remote connection
Array seamless scrolling demo
引擎国产化适配&重构笔记
Yolov6: the fast and accurate target detection framework is open source
ByteDance Interviewer: talk about the principle of audio and video synchronization. Can audio and video be absolutely synchronized?
Why is JSX syntax so popular?
Use of vim









