Sql Server 2000学习笔记(1)

最近在应聘过程中遇到了好多数据库方面的问题,搞不懂,于是决定好好学习一下Sql Server 2000。每学习一次我都要写一个学习笔记,就当做是我的学习总结,使自己明白这一次学到了什么,什么又没有学到.

下面是我在面试的时候遇到的问题,现在先总结一下:

1.电话明细表Phones(phone电话号码,startime开始通话时间,endtime结束时间)
月度总结表totals(phone电话号码,months月份,total总的通话时间,count通话次数)

现在从电话明细表中选择数据插入到月度总结表中
create table Phones(phone varchar(20), startime datetime, endtime datetime)
go
create table totals(phone varchar(20), months varchar(20), total int, [count] int)
go


insert into totals
select phone, months=convert(char(7), startime, 120), total=sum(datediff(second, startime, endtime)), [count]=count(*)
from Phones
group by phone, convert(char(7), startime, 120)


--没有考虑到通话时间跨月的问题

2.在Northwind数据库里面有四个表,分别是(仅列出会用到的字段):
1.Customers(CustomerID,CustomerName,...)
2.Orders(OrderID,CustomerID,OrderDate,...)
3.Products(ProductID,ProductName,UnitPrice,UnitsOnOrder,UnitsInStock,...)
4.Order Details(OrderID,ProductID,UnitPrice,Quantity,...)
现在要统计1998年度,对各个客户各个季度的销售总额:
即要得到如下结果:
CompanyName,ProductID,ProductName,第一季度,第二季度,第三季度,第四季度

select CustomerID, ProductID, datepart(qq, OrderDate),
第一季度=sum(case when datepart(qq, OrderDate)=1 then UnitPrice*Quantity else 0 end),
第二季度=sum(case when datepart(qq, OrderDate)=2 then UnitPrice*Quantity else 0 end),
第三季度=sum(case when datepart(qq, OrderDate)=3 then UnitPrice*Quantity else 0 end),
第四季度=sum(case when datepart(qq, OrderDate)=4 then UnitPrice*Quantity else 0 end)
from orders A
left join [Order Details] B on A.OrderID=B.OrderID
where year(OrderDate)=1998
group by CustomerID, ProductID, datepart(qq, OrderDate)

这些答案都是后来在csdn上热心的网友帮忙解答的,我从中也受益匪浅!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值