练习1
[sql]
view plain
copy
print
?
- select 单号 ,
- (
- case
- when 金额 >0 then 金额
- else 0
- end
- ) as 收入,
- (
- case
- when 金额 <0 then ABS( 金额)
- else 0
- end
- ) as 支出
- from T_1
<span style="font-size:14px;">select 单号 ,
(
case
when 金额 >0 then 金额
else 0
end
) as 收入,
(
case
when 金额 <0 then ABS( 金额)
else 0
end
) as 支出
from T_1</span>
练习二
2008/8/8 拜仁 胜
2008/8/9 奇才 胜
2008/8/9 湖人 胜
2008/8/10 拜仁 负
2008/8/8 拜仁 负
2008/8/12 奇才 胜
显示成
拜仁 1 2
湖人 1 0
奇才 2 0
下面是SQL语句:
[sql]
view plain
copy
print
?
- CREATE TABLE [T_Scores](
- [Date] [datetime] NULL,
- [Name] [nvarchar] (50) NULL,
- [Score] [nvarchar] (50) NULL
- );
- INSERT [T_Scores] ( [Date],[Name] ,[Score]) VALUES(CAST ('2008-08-08' as datetime),N '拜仁' ,N' 胜')
- INSERT [T_Scores] ( [Date],[Name] ,[Score]) VALUES(CAST ('2008-08-09' as datetime),N '奇才' ,N' 胜')
- INSERT [T_Scores] ( [Date],[Name] ,[Score]) VALUES(CAST ('2008-08-09' as datetime),N '湖人' ,N' 胜')
- INSERT [T_Scores] ( [Date],[Name] ,[Score]) VALUES(CAST ('2008-08-10' as datetime),N '拜仁' ,N' 负')
- INSERT [T_Scores] ( [Date],[Name] ,[Score]) VALUES(CAST ('2008-08-08' as datetime),N '拜仁' ,N' 负')
- INSERT [T_Scores] ( [Date],[Name] ,[Score]) VALUES(CAST ('2008-08-12' as datetime),N '奇才' ,N' 胜')
- SELECT Name ,
- SUM(
- CASE Score
- WHEN ' 胜' THEN 1
- ELSE 0
- END
- ) AS 胜,
- SUM(
- CASE Score
- WHEN ' 负' THEN 1
- ELSE 0
- END
- ) AS 负
- FROM T_Scores
- GROUP BY Name
<span style="font-size:14px;">CREATE TABLE [T_Scores](
[Date] [datetime] NULL,
[Name] [nvarchar] (50) NULL,
[Score] [nvarchar] (50) NULL
);
INSERT [T_Scores] ( [Date],[Name] ,[Score]) VALUES(CAST ('2008-08-08' as datetime),N '拜仁' ,N' 胜')
INSERT [T_Scores] ( [Date],[Name] ,[Score]) VALUES(CAST ('2008-08-09' as datetime),N '奇才' ,N' 胜')
INSERT [T_Scores] ( [Date],[Name] ,[Score]) VALUES(CAST ('2008-08-09' as datetime),N '湖人' ,N' 胜')
INSERT [T_Scores] ( [Date],[Name] ,[Score]) VALUES(CAST ('2008-08-10' as datetime),N '拜仁' ,N' 负')
INSERT [T_Scores] ( [Date],[Name] ,[Score]) VALUES(CAST ('2008-08-08' as datetime),N '拜仁' ,N' 负')
INSERT [T_Scores] ( [Date],[Name] ,[Score]) VALUES(CAST ('2008-08-12' as datetime),N '奇才' ,N' 胜')
SELECT Name ,
SUM(
CASE Score
WHEN ' 胜' THEN 1
ELSE 0
END
) AS 胜,
SUM(
CASE Score
WHEN ' 负' THEN 1
ELSE 0
END
) AS 负
FROM T_Scores
GROUP BY Name</span>
练习3
创建一张表,记录电话呼叫员的工作流水,记录呼叫员的编号,对方号码,通话开始时间,通话结束时间.
建表,插数据都自己写SQL语句
要求:输出所有数据中通话时间最长的5条记录.
输出所有数据中拨打长途号码(对方号码以0开头)的总时长
输出本月通话总时长最多的前三个呼叫员的编号
输出本月拨打号码次数最多的前三个呼叫员的编号
输出所有数据的拨号流水,并且在最后的一行添加总呼叫次数
-记录呼叫员编号,对方号码,通话时长
-...
-汇总[市内号码总时长][长途号码总时长]
SQL的代码:
[sql]
view plain
copy
print
?
- CREATE TABLE [T_PhoneWork] (
- [id] int identity (1, 1) primary key ,
- [WorkNum] [nvarchar] (50) NOT NULL,
- [PhoneNum] [nvarchar] (20) NULL,
- [StartTime] [datetime] NULL,
- [EndTime] [dateTime] NULL
- );
- INSERT INTO [T_PhoneWork]([WorkNum], [PhoneNum],[StartTime] ,[EndTime]) VALUES('100001' ,'13888888888', CAST('2013-1-1 7:10:10' AS datetime),CAST ('2013-1-1 7:20:10' AS datetime))
- INSERT INTO [T_PhoneWork]([WorkNum], [PhoneNum],[StartTime] ,[EndTime]) VALUES('100002' ,'13887888788', CAST('2013-1-1 8:10:10' AS datetime),CAST ('2013-1-1 8:30:10' AS datetime))
- INSERT INTO [T_PhoneWork]([WorkNum], [PhoneNum],[StartTime] ,[EndTime]) VALUES('100003' ,'13888688868', CAST('2013-1-1 7:20:10' AS datetime),CAST ('2013-1-1 8:20:10' AS datetime))
- INSERT INTO [T_PhoneWork]([WorkNum], [PhoneNum],[StartTime] ,[EndTime]) VALUES('100004' ,'13838883888', CAST('2013-1-1 8:10:10' AS datetime),CAST ('2013-1-1 10:20:10' AS datetime))
- INSERT INTO [T_PhoneWork]([WorkNum], [PhoneNum],[StartTime] ,[EndTime]) VALUES('100001' ,'13844888888', CAST('2013-1-1 10:10:10' AS datetime),CAST ('2013-1-1 12:20:10' AS datetime))
- INSERT INTO [T_PhoneWork]([WorkNum], [PhoneNum],[StartTime] ,[EndTime]) VALUES('100002' ,'013888885888', CAST('2013-1-1 13:16:10' AS datetime),CAST ('2013-1-1 14:20:10' AS datetime))
- INSERT INTO [T_PhoneWork]([WorkNum], [PhoneNum],[StartTime] ,[EndTime]) VALUES('100003' ,'13888588888', CAST('2013-1-1 14:10:10' AS datetime),CAST ('2013-1-1 16:20:10' AS datetime))
- INSERT INTO [T_PhoneWork]([WorkNum], [PhoneNum],[StartTime] ,[EndTime]) VALUES('100001' ,'013868886888', CAST('2013-1-1 12:10:10' AS datetime),CAST ('2013-1-1 17:20:10' AS datetime))
- INSERT INTO [T_PhoneWork]([WorkNum], [PhoneNum],[StartTime] ,[EndTime]) VALUES('100004' ,'13788878877', CAST('2013-1-1 14:10:10' AS datetime),CAST ('2013-1-1 16:20:10' AS datetime))
- INSERT INTO [T_PhoneWork]([WorkNum], [PhoneNum],[StartTime] ,[EndTime]) VALUES('100001' ,'13787878877', CAST('2013-1-1 16:10:10' AS datetime),CAST ('2013-1-1 18:20:10' AS datetime))
- INSERT INTO [T_PhoneWork]([WorkNum], [PhoneNum],[StartTime] ,[EndTime]) VALUES('100002' ,'13787878877', CAST('2013-1-1 12:10:10' AS datetime),CAST ('2013-1-1 14:20:10' AS datetime))
- INSERT INTO [T_PhoneWork]([WorkNum], [PhoneNum],[StartTime] ,[EndTime]) VALUES('100003' ,'13788078877', CAST('2013-1-1 16:10:10' AS datetime),CAST ('2013-1-1 17:20:10' AS datetime))
- INSERT INTO [T_PhoneWork]([WorkNum], [PhoneNum],[StartTime] ,[EndTime]) VALUES('100005' ,'13799078877', CAST('2013-5-1 12:10:10' AS datetime),CAST ('2013-5-2 22:20:10' AS datetime))
- CREATE TABLE [T_PhoneWork] (
- [id] int identity (1, 1) primary key ,
- [WorkNum] [nvarchar] (50) NOT NULL,
- [PhoneNum] [nvarchar] (20) NULL,
- [StartTime] [datetime] NULL,
- [EndTime] [dateTime] NULL
- );
- INSERT INTO [T_PhoneWork]([WorkNum], [PhoneNum],[StartTime] ,[EndTime]) VALUES('001' ,'020888888', CAST('2010-7-10 10:00:00' AS datetime),CAST ('2010-7-10 10:05:03' AS datetime))
- INSERT INTO [T_PhoneWork]([WorkNum], [PhoneNum],[StartTime] ,[EndTime]) VALUES('001' ,'020888888', CAST('2010-7-11 13:00:00' AS datetime),CAST ('2010-7-11 13:01:10' AS datetime))
- INSERT INTO [T_PhoneWork]([WorkNum], [PhoneNum],[StartTime] ,[EndTime]) VALUES('001' ,'8934343434', CAST('2010-7-11 14:06:00' AS datetime),CAST ('2010-7-11 14:09:00' AS datetime))
- INSERT INTO [T_PhoneWork]([WorkNum], [PhoneNum],[StartTime] ,[EndTime]) VALUES('002' ,'9837434777', CAST('2010-7-13 21:06:00' AS datetime),CAST ('2010-7-13 21:08:08' AS datetime))
- INSERT INTO [T_PhoneWork]([WorkNum], [PhoneNum],[StartTime] ,[EndTime]) VALUES('002' ,'0213434343', CAST('2010-6-29 20:11:00' AS datetime),CAST ('2010-6-29 20:16:06' AS datetime))
- INSERT INTO [T_PhoneWork]([WorkNum], [PhoneNum],[StartTime] ,[EndTime]) VALUES('001' ,'787878778', CAST('2010-7-15 13:16:00' AS datetime),CAST ('2010-7-15 13:26:00' AS datetime))
- INSERT INTO [T_PhoneWork]([WorkNum], [PhoneNum],[StartTime] ,[EndTime]) VALUES('003' ,'0334343444', CAST('2010-7-13 11:16:00' AS datetime),CAST ('2010-7-13 11:17:09' AS datetime))
- INSERT INTO [T_PhoneWork]([WorkNum], [PhoneNum],[StartTime] ,[EndTime]) VALUES('003' ,'676777333', CAST('2010-7-19 19:26:02' AS datetime),CAST ('2010-7-19 19:30:33' AS datetime))
- INSERT INTO [T_PhoneWork]([WorkNum], [PhoneNum],[StartTime] ,[EndTime]) VALUES('001' ,'89923434333', CAST('2010-6-19 15:16:02' AS datetime),CAST ('2010-6-19 15:26:10' AS datetime))
- INSERT INTO [T_PhoneWork]([WorkNum], [PhoneNum],[StartTime] ,[EndTime]) VALUES('004' ,'400400400', CAST('2010-6-19 15:16:02' AS datetime),CAST ('2010-6-19 15:26:10' AS datetime))
<span style="font-size:14px;">CREATE TABLE [T_PhoneWork] (
[id] int identity (1, 1) primary key ,
[WorkNum] [nvarchar] (50) NOT NULL,
[PhoneNum] [nvarchar] (20) NULL,
[StartTime] [datetime] NULL,
[EndTime] [dateTime] NULL
);
INSERT INTO [T_PhoneWork]([WorkNum], [PhoneNum],[StartTime] ,[EndTime]) VALUES('100001' ,'13888888888', CAST('2013-1-1 7:10:10' AS datetime),CAST ('2013-1-1 7:20:10' AS datetime))
INSERT INTO [T_PhoneWork]([WorkNum], [PhoneNum],[StartTime] ,[EndTime]) VALUES('100002' ,'13887888788', CAST('2013-1-1 8:10:10' AS datetime),CAST ('2013-1-1 8:30:10' AS datetime))
INSERT INTO [T_PhoneWork]([WorkNum], [PhoneNum],[StartTime] ,[EndTime]) VALUES('100003' ,'13888688868', CAST('2013-1-1 7:20:10' AS datetime),CAST ('2013-1-1 8:20:10' AS datetime))
INSERT INTO [T_PhoneWork]([WorkNum], [PhoneNum],[StartTime] ,[EndTime]) VALUES('100004' ,'13838883888', CAST('2013-1-1 8:10:10' AS datetime),CAST ('2013-1-1 10:20:10' AS datetime))
INSERT INTO [T_PhoneWork]([WorkNum], [PhoneNum],[StartTime] ,[EndTime]) VALUES('100001' ,'13844888888', CAST('2013-1-1 10:10:10' AS datetime),CAST ('2013-1-1 12:20:10' AS datetime))
INSERT INTO [T_PhoneWork]([WorkNum], [PhoneNum],[StartTime] ,[EndTime]) VALUES('100002' ,'013888885888', CAST('2013-1-1 13:16:10' AS datetime),CAST ('2013-1-1 14:20:10' AS datetime))
INSERT INTO [T_PhoneWork]([WorkNum], [PhoneNum],[StartTime] ,[EndTime]) VALUES('100003' ,'13888588888', CAST('2013-1-1 14:10:10' AS datetime),CAST ('2013-1-1 16:20:10' AS datetime))
INSERT INTO [T_PhoneWork]([WorkNum], [PhoneNum],[StartTime] ,[EndTime]) VALUES('100001' ,'013868886888', CAST('2013-1-1 12:10:10' AS datetime),CAST ('2013-1-1 17:20:10' AS datetime))
INSERT INTO [T_PhoneWork]([WorkNum], [PhoneNum],[StartTime] ,[EndTime]) VALUES('100004' ,'13788878877', CAST('2013-1-1 14:10:10' AS datetime),CAST ('2013-1-1 16:20:10' AS datetime))
INSERT INTO [T_PhoneWork]([WorkNum], [PhoneNum],[StartTime] ,[EndTime]) VALUES('100001' ,'13787878877', CAST('2013-1-1 16:10:10' AS datetime),CAST ('2013-1-1 18:20:10' AS datetime))
INSERT INTO [T_PhoneWork]([WorkNum], [PhoneNum],[StartTime] ,[EndTime]) VALUES('100002' ,'13787878877', CAST('2013-1-1 12:10:10' AS datetime),CAST ('2013-1-1 14:20:10' AS datetime))
INSERT INTO [T_PhoneWork]([WorkNum], [PhoneNum],[StartTime] ,[EndTime]) VALUES('100003' ,'13788078877', CAST('2013-1-1 16:10:10' AS datetime),CAST ('2013-1-1 17:20:10' AS datetime))
INSERT INTO [T_PhoneWork]([WorkNum], [PhoneNum],[StartTime] ,[EndTime]) VALUES('100005' ,'13799078877', CAST('2013-5-1 12:10:10' AS datetime),CAST ('2013-5-2 22:20:10' AS datetime))
CREATE TABLE [T_PhoneWork] (
[id] int identity (1, 1) primary key ,
[WorkNum] [nvarchar] (50) NOT NULL,
[PhoneNum] [nvarchar] (20) NULL,
[StartTime] [datetime] NULL,
[EndTime] [dateTime] NULL
);
INSERT INTO [T_PhoneWork]([WorkNum], [PhoneNum],[StartTime] ,[EndTime]) VALUES('001' ,'020888888', CAST('2010-7-10 10:00:00' AS datetime),CAST ('2010-7-10 10:05:03' AS datetime))
INSERT INTO [T_PhoneWork]([WorkNum], [PhoneNum],[StartTime] ,[EndTime]) VALUES('001' ,'020888888', CAST('2010-7-11 13:00:00' AS datetime),CAST ('2010-7-11 13:01:10' AS datetime))
INSERT INTO [T_PhoneWork]([WorkNum], [PhoneNum],[StartTime] ,[EndTime]) VALUES('001' ,'8934343434', CAST('2010-7-11 14:06:00' AS datetime),CAST ('2010-7-11 14:09:00' AS datetime))
INSERT INTO [T_PhoneWork]([WorkNum], [PhoneNum],[StartTime] ,[EndTime]) VALUES('002' ,'9837434777', CAST('2010-7-13 21:06:00' AS datetime),CAST ('2010-7-13 21:08:08' AS datetime))
INSERT INTO [T_PhoneWork]([WorkNum], [PhoneNum],[StartTime] ,[EndTime]) VALUES('002' ,'0213434343', CAST('2010-6-29 20:11:00' AS datetime),CAST ('2010-6-29 20:16:06' AS datetime))
INSERT INTO [T_PhoneWork]([WorkNum], [PhoneNum],[StartTime] ,[EndTime]) VALUES('001' ,'787878778', CAST('2010-7-15 13:16:00' AS datetime),CAST ('2010-7-15 13:26:00' AS datetime))
INSERT INTO [T_PhoneWork]([WorkNum], [PhoneNum],[StartTime] ,[EndTime]) VALUES('003' ,'0334343444', CAST('2010-7-13 11:16:00' AS datetime),CAST ('2010-7-13 11:17:09' AS datetime))
INSERT INTO [T_PhoneWork]([WorkNum], [PhoneNum],[StartTime] ,[EndTime]) VALUES('003' ,'676777333', CAST('2010-7-19 19:26:02' AS datetime),CAST ('2010-7-19 19:30:33' AS datetime))
INSERT INTO [T_PhoneWork]([WorkNum], [PhoneNum],[StartTime] ,[EndTime]) VALUES('001' ,'89923434333', CAST('2010-6-19 15:16:02' AS datetime),CAST ('2010-6-19 15:26:10' AS datetime))
INSERT INTO [T_PhoneWork]([WorkNum], [PhoneNum],[StartTime] ,[EndTime]) VALUES('004' ,'400400400', CAST('2010-6-19 15:16:02' AS datetime),CAST ('2010-6-19 15:26:10' AS datetime))</span>
1.输出所有数据中通话时间最长的5条记录.
[sql]
view plain
copy
print
?
- SELECT TOP 5 *FROM T_PhoneWork
- ORDER BY (DateDiff (s, StartTime,EndTime )) DESC
<span style="font-size:14px;">SELECT TOP 5 *FROM T_PhoneWork
ORDER BY (DateDiff (s, StartTime,EndTime )) DESC</span>
2.输出所有数据中拨打长途号码(对方号码以0开头)的总时长
[sql]
view plain
copy
print
?
- SELECT SUM (
- DateDiff(s ,StartTime, EndTime)
- ) AS 长途总时长
- FROM T_PhoneWork
- WHERE PhoneNum LIKE '0%'
<span style="font-size:14px;">SELECT SUM (
DateDiff(s ,StartTime, EndTime)
) AS 长途总时长
FROM T_PhoneWork
WHERE PhoneNum LIKE '0%'</span>
3.输出本月通话总时长最多的前三个呼叫员的编号
[sql]
view plain
copy
print
?
- SELECT TOP 3 WorkNum,count (*) FROM T_PhoneWork
- WHERE DateDiff (month, StartTime,getdate ()) = 0
- group by WorkNum
- order by sum( datediff(s ,StartTime, EndTime)) DESC
<span style="font-size:14px;">SELECT TOP 3 WorkNum,count (*) FROM T_PhoneWork
WHERE DateDiff (month, StartTime,getdate ()) = 0
group by WorkNum
order by sum( datediff(s ,StartTime, EndTime)) DESC</span>
[sql]
view plain
copy
print
?
- SELECT TOP 3 WorkNum FROM T_PhoneWork
- WHERE DateDiff (month, StartTime,'2010-7-2 9:12:00' ) = 0
- group by WorkNum
- order by sum( datediff(s ,StartTime, EndTime)) DESC
<span style="font-size:14px;">SELECT TOP 3 WorkNum FROM T_PhoneWork
WHERE DateDiff (month, StartTime,'2010-7-2 9:12:00' ) = 0
group by WorkNum
order by sum( datediff(s ,StartTime, EndTime)) DESC</span>
4.输出本月拨打号码次数最多的前三个呼叫员的编号
[sql]
view plain
copy
print
?
- select top 3 WorkNum from T_PhoneWork
- WHERE DateDiff (month, StartTime,getdate ()) = 0
- group by WorkNum
- order by count(*) DESC
<span style="font-size:14px;">select top 3 WorkNum from T_PhoneWork
WHERE DateDiff (month, StartTime,getdate ()) = 0
group by WorkNum
order by count(*) DESC</span>
5.注意,union的两个原则,类型要相容,这个容易犯错,谨记
输出所有数据的拨号流水,并且在最后的一行添加总呼叫次数
-记录呼叫员编号,对方号码,通话时长
-...
-汇总[市内号码总时长][长途号码总时长]
[sql]
view plain
copy
print
?
- select WorkNum as 呼叫员编号,PhoneNum as 对方号码 ,DateDiff( s,StartTime ,EndTime) as 通话时长
- from T_PhoneWork
- union all
- select ' 汇总',
- cast(
- sum(
- CASE substring (PhoneNum, 1,1 )
- when '0' then 0
- else DateDiff (s, StartTime,EndTime )
- end
- ) as nvarchar( 50)),
- cast(
- sum(
- CASE substring (PhoneNum, 1,1 )
- when '0' then DateDiff(s ,StartTime, EndTime)
- else 0
- end
- ) as nvarchar( 50))
- from T_PhoneWork
<span style="font-size:14px;">select WorkNum as 呼叫员编号,PhoneNum as 对方号码 ,DateDiff( s,StartTime ,EndTime) as 通话时长
from T_PhoneWork
union all
select ' 汇总',
cast(
sum(
CASE substring (PhoneNum, 1,1 )
when '0' then 0
else DateDiff (s, StartTime,EndTime )
end
) as nvarchar( 50)),
cast(
sum(
CASE substring (PhoneNum, 1,1 )
when '0' then DateDiff(s ,StartTime, EndTime)
else 0
end
) as nvarchar( 50))
from T_PhoneWork</span>
老师做的
[sql]
view plain
copy
print
?
- select WorkNum as 呼叫员编号,PhoneNum as 对方号码 ,DateDiff( s,StartTime ,EndTime) as 通话时长
- from T_PhoneWork
- union all
- select ' 汇总',
- cast(
- sum(
- CASE
- when PhoneNum not like '0%' then DateDiff( s,StartTime ,EndTime)
- else 0
- end
- ) as nvarchar( 50)),
- cast(
- sum(
- CASE
- when PhoneNum like '0%' then DateDiff (s, StartTime,EndTime )
- else 0
- end
- ) as nvarchar( 50))
- from T_PhoneWork
<span style="font-size:14px;">select WorkNum as 呼叫员编号,PhoneNum as 对方号码 ,DateDiff( s,StartTime ,EndTime) as 通话时长
from T_PhoneWork
union all
select ' 汇总',
cast(
sum(
CASE
when PhoneNum not like '0%' then DateDiff( s,StartTime ,EndTime)
else 0
end
) as nvarchar( 50)),
cast(
sum(
CASE
when PhoneNum like '0%' then DateDiff (s, StartTime,EndTime )
else 0
end
) as nvarchar( 50))
from T_PhoneWork</span>
30.索引
全表扫描:对数据进行检索(select)效率最差的是全表扫描,就是一条条的找
如果没有目录,查汉语字典就要一页页的翻,而有了目录只要查目录即可.为了提高检索速度,可以为经常进行检索的列添加索引,相当于创建目录
建立索引优点:检索速度快
缺点:占磁盘空间,降低编辑表的操作,如insert,update
(*)即使创建了索引,仍然有可能全表扫描,比如like,函数,类型转换等
31.表连接jion 主流不增加外键
有客户表 和 订单表 ,关联查询 [table1] as [别名] join [table2] as [别名] on [约束条件]