a、一个学生成绩表中有scoreId、studentId、english、math等几个四段。完成以下练习:
--要求在一个表格中查询出学生的英语最高成绩、最低成绩和平均成绩
方法1:select '最高成绩:'+cast(max(english) as varchar(50)) 统计结果 from score union all select '最低成绩:'+cast(min(english) as varchar(50)) from score union all select '平均成绩:'+cast(avg(english) as varchar(50)) from score
方法2:select '最高成绩:' as 统计结果, max(english) as 成绩 from score union all
select '最低成绩:' , min(english) from score union all
select '平均成绩:', avg(english) from score
b、有一个教师表,教师的姓名、工资,并且在最后一行加上平均工资和最高工资:
select 姓名=tname,工资=tsalary from teacher union all select '平均工资',avg(tsalary) from teacher union all select '最高工资', max(tsalary) from teacher
2、字符串函数
LEN():计算字符串长度(字符的个数)。
LOWER(): 转小写 UPPER():转大写
LTRIM:去掉字符串左边空格 RTRIM():去掉字符串右边空格
LEFT(参数1,参数2)、RIGHT(参数1,参数2) :截取字符串,参数1表示要被截取的字符串,参数2表示 截取长度(从1开始),如:LEFT(‘HELLO’,2) 结果为:HE
SUBSTRING(参数1,参数2,参数3):参数1表示要被截取的字符串,参数2表示从第几个开始截取,参数3表示要截取的长度。如:SUBSTRING('HELLO',2,2) 结果为:EL
3、日期函数
getdate():获取当前系统时间,year(getdate()):获取当前年份,month(getdate()):获取当前月份,day(getdate()):获取当前是本月第几天。dateadd(参数1,参数2,参数3):c参数1表示在哪部分日期上加,参数2表示要加多少,参数3表示被加的日期。计算时间差:datediff(参数1,参数2,参数3):参数1表示要计算的时间部分,参数2表示起始时间,参数3表示终止时间。