【經驗積累】SQL語句

 表中有字段dfhm(對方號碼),thsc(通話時長),ID

要求統計出所有記錄中,和哪些人的通話中有60%以上的通話時間不足5秒的對方號碼,總通話次數,不足5秒的次數 ,比率,平均通話時長

查詢結果字段如下:    對方號碼   總次數   超短次數   比率   平均時長

一下是SQL的設計,從分解完成到最終完成

select t.DFHM ,count(id) from T_ANALYS_PHONE_DETAIL t
  group by dfhm;

 

select dfhm from T_ANALYS_PHONE_DETAIL
       where thsc<5;

 

select dfhm,count(id) 超短次數 from T_ANALYS_PHONE_DETAIL
       where thsc<5
 group by dfhm;

 

select t.DFHM ,count(t.id) 總次數 from T_ANALYS_PHONE_DETAIL t
 where t.dfhm in(
  select dfhm from T_ANALYS_PHONE_DETAIL
          where thsc<5
   group by dfhm
 )
 group by dfhm;

 

select zctj.d 對方號碼,zctj.cou 總次數,cdtj.cou 超短次數  from
  (
    select dfhm d,count(id) cou from T_ANALYS_PHONE_DETAIL
      where dfhm in(
        select dfhm from T_ANALYS_PHONE_DETAIL
                 where thsc<5
          group by dfhm
      )
     group by dfhm
  )zctj,
  (
    select dfhm d,count(id) cou from T_ANALYS_PHONE_DETAIL
             where thsc<5
    group by dfhm
  )cdtj
where zctj.d=cdtj.d;

 

select zctj.d 對方號碼,zctj.cou 總次數,cdtj.cou 超短次數  from
  (
    select dfhm d,count(id) cou from T_ANALYS_PHONE_DETAIL
      where dfhm in(
        select dfhm from T_ANALYS_PHONE_DETAIL
                 where thsc<5
          group by dfhm
      )
     group by dfhm
  )zctj,
  (
    select dfhm d,count(id) cou from T_ANALYS_PHONE_DETAIL
             where thsc<5
    group by dfhm
  )cdtj
where zctj.d=cdtj.d and cdtj.cou/zctj.cou>=0.6;

 

select zctj.d 對方號碼,zctj.cou 總次數,cdtj.cou 超短次數,cdtj.cou/zctj.cou 比率 from
  (
    select dfhm d,count(id) cou from T_ANALYS_PHONE_DETAIL
      where dfhm in(
        select dfhm from T_ANALYS_PHONE_DETAIL
                 where thsc<5
          group by dfhm
      )
     group by dfhm
  )zctj,
  (
    select dfhm d,count(id) cou from T_ANALYS_PHONE_DETAIL
             where thsc<5
    group by dfhm
  )cdtj
where zctj.d=cdtj.d and cdtj.cou/zctj.cou>=0.6;

 

select zctj.d 對方號碼,zctj.cou 總次數,cdtj.cou 超短次數,cdtj.cou/zctj.cou 比率,zctj.thsc/zctj.cou 平均時長 from
  (
    select dfhm d,count(id) cou,sum(thsc) thsc from T_ANALYS_PHONE_DETAIL
      where dfhm in(
        select dfhm from T_ANALYS_PHONE_DETAIL
                 where thsc<5
          group by dfhm
      )
     group by dfhm
  )zctj,
  (
    select dfhm d,count(id) cou from T_ANALYS_PHONE_DETAIL
             where thsc<5
    group by dfhm
  )cdtj
where zctj.d=cdtj.d and cdtj.cou/zctj.cou>=0.6;

 

select zctj.d 對方號碼,zctj.cou 總次數,cdtj.cou 超短次數,round(cdtj.cou/zctj.cou,2) 比率,round(zctj.thsc/zctj.cou,1) 平均時長 from
  (
    select dfhm d,count(id) cou,sum(thsc) thsc from T_ANALYS_PHONE_DETAIL
      where dfhm in(
        select dfhm from T_ANALYS_PHONE_DETAIL
                 where thsc<5
          group by dfhm
      )
     group by dfhm
  )zctj,
  (
    select dfhm d,count(id) cou from T_ANALYS_PHONE_DETAIL
             where thsc<5
    group by dfhm
  )cdtj
where zctj.d=cdtj.d and cdtj.cou/zctj.cou>=0.6;

發佈了8 篇原創文章 · 獲贊 0 · 訪問量 2萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章