2008R2 SQL數據庫實例查詢

我們以電信的收費系統爲例的SQL數據庫查詢:

數據庫biao1 裏面有幾個表:call、 call-history

1、-------從biao1的數據庫裏查詢表call的全部信息-------

use biao1

select * from call

clip_image002

2、將查詢的列名翻譯爲你知道的漢語

select id '編號',starttime '開始時間',endtime '結束時間',phonenumber '電話號碼',calltype '電話類型' from call

clip_image004

3、查詢某一電話號碼出現的總數

select COUNT(*)from call where phonenumber=13001013336

clip_image005

4、查詢在某時間段內所有電話號碼包含“333”的長途,並按開始時間降序排列

select * from call where phonenumber like '%333%' and starttime between '2010-08-01 00:00:00' and '2010-08-05 23:23:23' and calltype=1

order by starttime DESC

clip_image007

5、查詢某一電話號碼在某時間段內的數據

select * from call where starttime&gt;'2010-08-01 00:00:00' and starttime<='2010-08-06 00:00:00' and phonenumber=13001013336

clip_image009

6、查詢某一號碼的市話費用總和

clip_image010

select sum(DATEDIFF(mi,starttime,endtime))*0.2 as '市話費'

from call where calltype=0 and phonenumber=13001013336

7、查找某電話的長途通話中最大通話時間是多少

select max(DATEDIFF(mi,starttime,endtime))as '長途最大通話時間'

from call where calltype=1 and phonenumber=13001013336

clip_image011

8、查詢某電話所有市話裏通話最短的時間

select min(DATEDIFF(mi,starttime,endtime))as '市話最小通話時間'

from call where calltype=0 and phonenumber=13001013336

clip_image013

9、查詢某電話所有市話的平均話費

select '用戶13001013336的平均市話費爲:' + cast((avg(DATEDIFF(mi,starttime,endtime))*0.2)as varchar(10))as '電話費'

from call where calltype=0 and phonenumber=13001013336

clip_image015

10、從表call和表call-history中查找電話號碼相同的,且符合某些條件的數據

select * from call INNER JOIN [call-history] ON CALL.phonenumber=[call-history].phonenumber

where call.calltype=0 and [call-history].calltype=1 and call.id&lt;100010 and [call-history].id>100015

clip_image017

11、把所有數據複製到表a 中

select * into a from call

clip_image018

12、把查詢到的結果寫入到表b中

select call.phonenumber into b from call

where starttime between '2010-08-01 00:00:00' and '2010-08-05 23:23:23' and calltype=0

clip_image020

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章