一道與時間差有關的SQL面試題

2021-09-08 20:00:48 字數 3780 閱讀 4758

題目:

一組聯絡歷史(總共500萬條):

id 主叫號碼 被叫號碼 通話起始時間 通話結束時間 通話時長

1 98290000 0215466546656 2007-02-01 09:49:53.000 2007-02-01 09:50:16.000 23

2 98290000 021546654666 2007-02-01 09:50:29.000 2007-02-01 09:50:41.000 12

3 98290000 021546654666 2007-02-01 09:50:58.000 2007-02-01 09:51:12.000 14

4 68290900 0755133329866 2007-02-01 10:04:31.000 2007-02-01 10:07:13.000 162

5 78290000 0755255708638 2007-02-01 10:48:26.000 2007-02-01 10:49:23.000 57

6 78290000 0755821119109 2007-02-01 10:49:39.000 2007-02-01 10:52:55.000 196

7 78290000 035730928370 2007-02-01 11:30:45.000 2007-02-01 11:31:58.000 73

8 78290000 0871138889904 2007-02-01 11:33:47.000 2007-02-01 11:35:00.000 73

9 68290000 035730928379 2007-02-01 11:52:20.000 2007-02-01 11:54:56.000 156

10 68290000 0298521811199 2007-02-01 12:44:45.000 2007-02-01 12:45:04.000 19

求其中同乙個號碼的兩次通話之間間隔大於10秒的聯絡歷史id

例如:6,7,8,9,10條記錄均符合。

create table phone (

id number,

zph number,

bph number,

pbegin date,

pend date

insert into phone values(1,98290000,0215466546656,to_date('2007-02-01 09:49:53','yyyy-mm-dd hh24:mi:ss'),to_date('2007-02-01 09:50:16','yyyy-mm-dd hh24:mi:ss'));

insert into phone values(2,98290000,021546654666,to_date('2007-02-01 09:50:29','yyyy-mm-dd hh24:mi:ss'),to_date('2007-02-01 09:50:41','yyyy-mm-dd hh24:mi:ss'));

insert into phone values(3,98290000,021546654666,to_date('2007-02-01 09:50:58','yyyy-mm-dd hh24:mi:ss'),to_date('2007-02-01 09:51:12','yyyy-mm-dd hh24:mi:ss'));

insert into phone values(4,68290900,0755133329866,to_date('2007-02-01 10:04:31','yyyy-mm-dd hh24:mi:ss'),to_date('2007-02-01 10:07:13','yyyy-mm-dd hh24:mi:ss'));

insert into phone values(5,78290000,0755255708638,to_date('2007-02-01 10:48:26','yyyy-mm-dd hh24:mi:ss'),to_date('2007-02-01 10:49:23','yyyy-mm-dd hh24:mi:ss'));

insert into phone values(6,78290000,0755821119109,to_date('2007-02-01 10:49:39','yyyy-mm-dd hh24:mi:ss'),to_date('2007-02-01 10:52:55','yyyy-mm-dd hh24:mi:ss'));

insert into phone values(7,78290000,035730928370,to_date('2007-02-01 11:30:45','yyyy-mm-dd hh24:mi:ss'),to_date('2007-02-01 11:31:58','yyyy-mm-dd hh24:mi:ss'));

insert into phone values(8,78290000,0871138889904,to_date('2007-02-01 11:33:47','yyyy-mm-dd hh24:mi:ss'),to_date('2007-02-01 11:35:00','yyyy-mm-dd hh24:mi:ss'));

insert into phone values(9,68290000,035730928379,to_date('2007-02-01 11:52:20','yyyy-mm-dd hh24:mi:ss'),to_date('2007-02-01 11:54:56','yyyy-mm-dd hh24:mi:ss'));

insert into phone values(10,68290000,0298521811199,to_date('2007-02-01 12:44:45','yyyy-mm-dd hh24:mi:ss'),to_date('2007-02-01 12:45:04','yyyy-mm-dd hh24:mi:ss'));

commit;

sql> select * from phone;

id        zph        bph pbegin      pend

1   98290000 2154665466 2007/2/1 9: 2007/2/1 9:

2   98290000 2154665466 2007/2/1 9: 2007/2/1 9:

3   98290000 2154665466 2007/2/1 9: 2007/2/1 9:

4   68290900 7551333298 2007/2/1 10 2007/2/1 10

5   78290000 7552557086 2007/2/1 10 2007/2/1 10

6   78290000 7558211191 2007/2/1 10 2007/2/1 10

7   78290000 3573092837 2007/2/1 11 2007/2/1 11

8   78290000 8711388899 2007/2/1 11 2007/2/1 11

9   68290000 3573092837 2007/2/1 11 2007/2/1 11

10   68290000 2985218111 2007/2/1 12 2007/2/1 12

10 rows selected

sql> select t1.id

2    from (select rownum rm, t.* from phone t) t1,

3         (select rownum rm, t.* from phone t) t2

4   where t1.zph = t2.zph

5     and t1.rm = t2.rm + 1

6     and (t1.pbegin - t2.pend)*24*60*60 > 10;

id6 rows selected

說明:oracle中date型別資料「+、-」操作返回值單位為「day」。

一道有關StringBuffer的面試題

public class test public static void main string args 2 現在dosomething 方法中 除去 buff new stringbuffer public class test public static void main string ar...

一道sql 的面試題

表結構 成績表 grade 包含字段 gradeid int,自增 sno int,學號 cno int,課程號 score float,分數 查詢每門課程的平均 最高 最低 分及課程號 select g score cno from dbtest dbo grade group by cno se...

一道sql面試題的解答

題目 寫出一條sql語句 取出表a中第31到第40記錄 sqlserver,以自動增長的id作為主鍵,注意 id可能不是連續的。解答 已測試 1 假設id是連續的 select top10 fromawhereidnot in selecttop30 idfroma 或select fromawhe...