SQL語句練習

2021-05-23 11:45:53 字數 2461 閱讀 4780

建立一張表,記錄**呼叫員的工作流水,記錄呼叫員編號,對方號碼,通話開始時間,

結束時間。建表,插資料等都自己寫出sql**

要求:輸出所有資料中通話時間最長的5條記錄。

輸出所有資料中撥打長途號碼(對方號碼以0開頭)的總時長

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

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

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

記錄呼叫員編號,對方號碼,通話時長

。。。。。。

總匯【市內號碼總時長】【長途號碼總時長】

create table t_phone(id int not null,

caller nvarchar(20) not null,tonumber varchar(20) not null,

starttime datetime not null,endtime datetime not null)

insert into t_phone(caller,tonumber,starttime,endtime)

values('009','15358168859',getdate(),dateadd(ss,513,dateadd(mm,8,getdate())))

update t_phone set endtime =

dateadd(month,abs(datepart(month,endtime)-datepart(month,starttime)),endtime)

where datediff(month,starttime,endtime)<0

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

select top 5 *,datediff(second,starttime,endtime)as n'持續時間' from t_phone

order by datediff(second,starttime,endtime) desc

輸出所有資料中撥打長途號碼(對方號碼以5開頭)的總時長

select sum(cast(datediff(second,starttime,endtime) as int))

from t_phone

where substring(tonumber,1,1)='5'

或者where tonumber like '5%'

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

select top 3 caller,sum(cast(datediff(second,starttime,endtime) as int))as n'總時長'

from t_phone

where datediff(year,starttime,getdate())=0 and

datediff(month,starttime,getdate())=0

group by caller

order by sum(cast(datediff(second,starttime,endtime) as int)) desc

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

select top 2 caller,count(*)as n'次數'

from t_phone

where datediff(year,starttime,getdate())=0 and

datediff(month,starttime,getdate())=0

group by caller

order by count(*) desc

記錄呼叫員編號,對方號碼,通話時長

。。。。。。

總匯【市內號碼總時長】【長途號碼總時長】

select

caller,

sum(

case

when tonumber like '2%' then datediff(second,starttime,endtime)

else 0

end)as n'長途',

sum(

case

when tonumber not like '2%' then datediff(second,starttime,endtime)

else 0

end)as n'市內'

from t_phone

group by caller

union

select

n'彙總',

sum(

case

when tonumber like '2%' then datediff(second,starttime,endtime)

else 0

end)as n'長途',

sum(

case

when tonumber not like '2%' then datediff(second,starttime,endtime)

else 0

end)as n'市內'

from t_phone

SQL 語句練習

mysql select from persons limit 5 oracle select from persons where rownum 5 select from persons where name like l select from persons where name not l...

SQL語句練習

1 把兩張 的資料抽取出來放到另外一張 中 1 pt表 role id int pt int 2 season score表 role id int season score int 3 player表 role id int pt int season score int count int 4 ...

sql語句練習

select instr xtr x from dual t instr,函式,返回某字元出現的位置 select instr syran ma a 1,2 from dual select to char sysdate,yyyy mm dd hh24 mi ss from dual select...