sql常用查詢

2022-04-03 17:50:21 字數 2128 閱讀 6622

insert

into t_call(callnumber,tellnum,startdatetime,enddatetime) values('

13925473495

','gzsx009

','2013-10-20 10:03

','2013-10-22 13:06')

--輸出所有資料中通話時間最長的5條記錄。用orderby datediff

select

top5

*from t_call order

bydatediff(second,startdatetime,enddatetime) desc

--輸出所有資料中撥打長途**的總時長。(0開頭為長途) like,sum

select

sum(datediff(second, startdatetime, enddatetime))as

'長途通話時長

'from t_call where callnumber like'0%

'--輸出本月通話總時長最多的前三呼叫員的編號

--datediff(month..)=0表示本月

select

top3 tellnum from

t_call

where

datediff(month, startdatetime, getdate())=

0group

bytellnum

order

bysum(datediff(second, startdatetime, enddatetime)) desc

--輸出本月撥打**次數最多的前三個呼叫員的編號

select

top3 tellnum,count(*) as n'

通話次數

'from t_call where

datediff(month, startdatetime, enddatetime)=

0group

bytellnum

order

by 通話次數 desc

--輸出所有資料的撥號流水,並且在最後一行新增呼叫總時長

select tellnum,callnumber,datediff(second, startdatetime, enddatetime) as 通話時長 from

t_call

union

allselectn'

彙總',convert(nvarchar(50

),sum

((case

when callnumber like'0%

'then

datediff

(second, startdatetime, enddatetime)

else

0end

))),

sum((

case

when callnumber not

like'0%

'then

datediff

(second, startdatetime, enddatetime)

else

0end

))from

t_call

where

datediff(month, startdatetime, enddatetime)=0--

計費表jifei

--查詢6,7,8,月份有話費產生,但是9,10月份沒有使用,並且6,7,8話費均在51-100之間

select

distinct phone from

jifei

where

phone in(

select phone from jifei where

months

in (6,7,8)and expense between

51and

100)

and phone notin(

select phone from jifei where months in(10,11) and expense=0)

--

常用SQL查詢指令碼

本文假定讀者已經對資料庫連線技術有所了解,因此只討論有關sql查詢命令的語法。表結構如下 ms access 2000 表名 usertable 欄位名 字段型別 userid 自動計數 長整型 username 文字 user 文字 createdate 日期 時間 預設值為now 一 用sele...

簡單常用sql查詢

self.db executeupdate sql,record.recordid create table scene record id text primary key,record time text,location text,place text,description text,isu...

SQL常用條件查詢語句

select from table1 where 工資 2500 and 工資 3000 同上 select 姓名 from table1 where 性別 0 and 工資 4000 select from table1 where not 工資 3200 select from table1 o...