SQL server 第十一章-----连接查询和分组查询课后简答题
在前面的NetBar数据库中编写查询语句分别满足以下要求。
–一位家长想看看他儿子这个月的上机次数,他儿子的卡号为0023ABC。
select CardID, count(1) as 次数
from Record
where CardID=‘0024_ABD’
Group by CardID
go
–查询24小时之内上机的人员姓名列表。
select a.[UserNmae]
from Card as a
inner join Record as b
on b.CardID=a.ID
where datediff(hh,b.BeginTime,getdate())<24
go
– 查询本周上机人员的姓名,计算机名,总费用,并按照姓名进行分组。
select a.UserNmae as 姓名, b.ComputerID as 计算机, b.Fee as 金额
from Card as a
inner join Record as b
on a.ID=b.CardID
where datepart(week,getdate())=datepart(week,b.BeginTime) and year(getdate())=year( b.BeginTime)
go
–datepart日期中指定日期部分的整数形式
–week周
–getdate,当前系统日期
–year年
– 查询卡号必要没和第七位是ab的人员的消费情况。
select a.UserN