function
Part I: Functions 1
- Aggregate function
- sum()
sum, accumulate and sum the data in the specified field, and can only perform statistics on numeric fields. - avg()
calculates the average value and counts the average value in the specified field. - max()
Maximum value, count the maximum value in the specified field, if there are the same values, the first one will be displayed in physical order. - min()The
minimum value, which counts the minimum value in the specified field. - count()
find the number, count the number of data records in the specified field or in the results that meet the query conditions - Examples:
- Query the number of employees with more than 10 years of service
select dang_an.name, count(gl) as 人数 from dang_an where gl>10 group by dang_an.name
- Query the number of male and female employees
select dang_an.sex,count(dang_an.sex) as 人数 from dang_an group by sex
- Query the average salary and average assessment score of each employee
select dang_an.name,avg(gongzi.gongzi)as 平均工资,avg(kaohe.kaohe)as 平均考核分 from dang_an,gongzi,kaohe group by dang_an.name
- Query the total salary and total performance of each employee in the sales department
select dang_an.name,sum(gongzi.gongzi)as 总工资,sum(yeji.yeji)as 总业绩 from dang_an,zhiwei,gongzi,yeji where dang_an.zw_id=zhiwei.zw_id and dang_an.id=gongzi.id and dang_an.id=yeji.id and gongzi.nian=yeji.nian and gongzi.yue=yeji.yue and zhiwei.zw='销售' group by dang_an.name
- Query all wages, maximum wages and minimum wages of each employee, displayed in the following form: name, highest, lowest
select dang_an.name as 姓名,max(gongzi.gongzi)as 最高,min(gongzi.gongzi)as 最低 from dang_an,gongzi where dang_an.id=gongzi.id group by dang_an.name
- Date function
- GetDate() Get the current system date
SELECT GETDATE()
返回:当前日期和时间
- DateAdd() Add the specified year (yy), month (mm) or day (dd)
SELECT DATEADD(mm,4,'2017-12-20')
返回:2018-04-20(增加4个月)
- DateDiff() compares the difference of the specified date part between two dates
SELECT DATEDIFF(yy,'2000-12-20','2017-06-16')
返回:17(相差17年)
- DateName() displays a string of a specific part of the specified date
SELECT DATENAME(DW,'2017-06-16')
返回:星期五(dw是星期)
- DatePart() displays the integer form of the specified date part in the date
SELECT DATEPART(mm,'2017-06-16')
返回:6(六月)
- Small expansion
- Query how many days you have lived
select DATEDIFF(dd,'xxxx-xx-xx',GETDATE()) - Query the day of the week when each employee was born
select dang_an.name, DATENAME(dw,dang_an.csny)from dang_an