当前位置:网站首页>SQL Server AVG函数取整问题
SQL Server AVG函数取整问题
2022-06-24 09:40:00 【Cpsu】
SQL Sever的均值函数在求整数类型的数据均值是结果还是整数,并且不会四舍五入。也就是向下取整。这里先创建一个成绩表,并适当添加数据。
CREATE TABLE sic(
学号 nchar(10) not null,
课程号 nchar(10) not null constraint pk_lh primary key(学号,课程号),
成绩 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(成绩) AS 均分
FROM sic
GROUP BY sic.学号

很明显学号为01的学生均分不应该为整数。这里有两个办法可以解决。
第一个就是在创建表的时候就将数据设置为float类型。
ALTER TABLE sic
ALTER COLUMN 成绩 FLOAT NOT NULL;
SELECT sic.学号,AVG(成绩) AS 均分
FROM sic
GROUP BY sic.学号;
#也可以使用DECIMAL(18,2)格式保留两位小数

第二种方法就是利用SQL Server的CAST或者CONVERT函数。
SELECT sic.学号,
AVG(
CAST (成绩 AS FLOAT)
) AS 均分
FROM sic
GROUP BY sic.学号
SELECT CAST(AVG(CONVERT(FLOAT,成绩)) AS DECIMAL(18,2)) AS 均分
FROM sic
GROUP BY sic.学号
边栏推荐
- Oracle viewing data file header SCN information
- Tnsnames Ora file configuration
- Development of anti fleeing marketing software for health products
- Honeypot 2 hfish, ehoney
- PHP uses recursive and non recursive methods to create multi-level folders
- Floating point notation (summarized from cs61c and CMU CSAPP)
- YOLOv6:又快又准的目标检测框架开源啦
- Graffiti smart brings a variety of heavy smart lighting solutions to the 2022 American International Lighting Exhibition
- [Eureka source code analysis]
- Baidu AI template for knowledge understanding
猜你喜欢

impdp导schema报ORA-31625异常处理

Tutorial (5.0) 08 Fortinet security architecture integration and fortixdr * fortiedr * Fortinet network security expert NSE 5

生产者/消费者模型

Three ways to use applicationcontextinitializer

队列Queue

Practical analysis: implementation principle of APP scanning code landing (app+ detailed logic on the web side) with source code

有关二叉树 的基本操作

411-栈和队列(20. 有效的括号、1047. 删除字符串中的所有相邻重复项、150. 逆波兰表达式求值、239. 滑动窗口最大值、347. 前 K 个高频元素)

ByteDance Interviewer: talk about the principle of audio and video synchronization. Can audio and video be absolutely synchronized?

《MATLAB 神经网络43个案例分析》:第32章 小波神经网络的时间序列预测——短时交通流量预测
随机推荐
TP5 using post to receive array data times variable type error: solution to array error
Arbre binaire partie 1
Dragging El table sortablejs
SSH Remote Password free login
100 GIS practical application cases (XIV) -arcgis attribute connection and using Excel
2021-08-17
Analysis of 43 cases of MATLAB neural network: Chapter 32 time series prediction of wavelet neural network - short-term traffic flow prediction
PHP encapsulates a file upload class (supports single file and multiple file uploads)
NVIDIA's CVPR 2022 oral is on fire! 2D images become realistic 3D objects in seconds! Here comes the virtual jazz band!
Error reading CSV (TSV) file
CICFlowMeter源码分析以及为满足需求而进行的修改
Getting user information for applet learning (getuserprofile and getUserInfo)
Practical analysis: implementation principle of APP scanning code landing (app+ detailed logic on the web side) with source code
Five heart matchmaker
How large and medium-sized enterprises build their own monitoring system
Tnsnames Ora file configuration
5 minutes, excellent customer service chat handling skills
队列Queue
涂鸦智能携多款重磅智能照明解决方案,亮相2022美国国际照明展
读取csv(tsv)文件出错