Sql时间函数
一、sql server日期时间函数
Sql Server中的日期与时间函数
1. 当前系统日期、时间
select getdate()
2. dateadd 在向指定日期加上一段时间的基础上,返回新的 datetime 值
例如:向日期加上2天
select dateadd(day,2,'2004-10-15') --返回:2004-10-17 00:00:00.000
3. datediff 返回跨两个指定日期的日期和时间边界数。
select datediff(day,'2004-09-01','2004-09-18') --返回:17
4. datepart 返回代表指定日期的指定日期部分的整数。
select DATEPART(month, '2004-10-15') --返回 10
5. datename 返回代表指定日期的指定日期部分的字符串
select datename(weekday, '2004-10-15') --返回:星期五
6. day(), month(),year() --可以与datepart对照一下
select 当前日期=convert(va ......
sql统计-关于学生成绩
http://blog.sina.com.cn/s/blog_61380b320100ej9k.html
sql统计-关于学生成绩
学生成绩表(stuscore):
姓名:name
课程:subject
分数:score
学号:stuid
张三
数学
89
1
张三
语文
80
1
张三
英语
70
1
李四
数学
90
2
李四
语文
70
2
李四
英语
80
2
创建表
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[stuscore](
[name] [varchar](50) COLLATE Chinese_PRC_CI_AS NULL,
[subject] [varchar](50) COLLATE Chinese_PRC_CI_AS NULL,
[score] [int] NULL,
[stuid] [int] NULL
) ON [PRIMARY]
GO
SET ANSI_PADDING OFF
问题:
1. 计算每个人的总成绩并排名(要求显示字段:姓名,总成绩)
2. 计算每个人的总成绩并排名(要求显示字段: 学号,姓名,总 ......
sql统计-关于学生成绩(答案)
http://blog.sina.com.cn/s/blog_61380b320100ej9p.html
答案:
1. 计算每个人的总成绩并排名
select name,sum(score) as allscore from stuscore group by name order by allscore
2. 计算每个人的总成绩并排名
select distinct t1.name,t1.stuid,t2.allscore from stuscore t1,
(
select stuid,sum(score) as allscore from stuscore group by stuid
)t2
where t1.stuid=t2.stuid
order by t2.allscore desc
3. 计算每个人单科的最高成绩
select t1.stuid,t1.name,t1.subject,t1.score from stuscore t1,
(
select stuid,max(score) as maxscore from stuscore group by stuid
) t2
where t1.stuid=t2.stuid and t1.score=t2.maxscore
4.计算每个人的平均成绩
select distinct t1.stuid,t1.n ......
加[ ]用于定界列名或表名等信息,以区别一些特殊的不合命名规则的字串,或是与系统保留字有冲突。
如"user"是SQL Server保留的关键字,所以用"select * from user"查询会提示语法错误。加上中括号,以声明其不是保留字。
"select * from [user]"
如果表名不是关键字,如Company,则用"select * from Company"是没问题的,不用加中括号,当然最好的办法是表名统统加方括号
[] 里的内容表示是一个用户级的变量.
目的是防止用户使用的名称和系统保留字冲突
比如你有一个表名就叫select
那么你 select * from select肯定是不行的.
但是你select * from [select] 就没问题了
你那个 user也是一样.
所有的表名字段名都可以加[],但是只要不跟系统关键字冲突就可以省略
......
加[ ]用于定界列名或表名等信息,以区别一些特殊的不合命名规则的字串,或是与系统保留字有冲突。如"user"是SQL Server保留的关键字,所以用"select * from user"查询会提示语法错误。加上中括号,以声明其不是保留字。 "select * from [user]" 如果表名不是关键字,如Company,则用"select * from Company"是没问题的,不用加中括号,当然最好的办法是表名统统加方括号 [] 里的内容表示是一个用户级的变量. 目的是防止用户使用的名称和系统保留字冲突比如你有一个表名就叫select 那么你 select * from select肯定是不行的. 但是你select * from [select] 就没问题了你那个 user也是一样. 所有的表名字段名都可以加[],但是只要不跟系统关键字冲突就可以省略 ......
1、查找表中多余的重复记录,重复记录是根据单个字段(peopleId)来判断
select * from people
where peopleId in (select peopleId from people group by peopleId having count
(peopleId) > 1)
2、删除表中多余的重复记录,重复记录是根据单个字段(peopleId)来判断,只留有rowid最小的记录
delete from people
where peopleId in (select peopleId from people group by peopleId having count
(peopleId) > 1)
and rowid not in (select min(rowid) from people group by peopleId having count(peopleId
)>1)
3、查找表中多余的重复记录(多个字段)
select * from vitae a
where (a.peopleId,a.seq) in (select peopleId,seq from ......