項目中比較實用的統計查詢

來源:互聯網
上載者:User

情景:假如有一個登陸日誌表,表名為sys_loginlog,該表記錄的是使用者使用系統的情況,有一個欄位叫logtime,即登陸時間。問題如下:

1.統計某一年各個月使用者登入系統的情況。

2.統計某一個月使用者登入系統的情況。

3.統計某一天各個時間段使用者登入系統的情況。

答案如下,分別使用了oracle 和 sqlserver文法。 Code: /**統計一年中每月的登入次數oracl */     select  count(*) as sum ,substr(to_char(t.logintime,'YYYY-MM'),6,2) as month     from  Mwpm_Sys_Loginlog  t   where       to_char(t.logintime,'YYYY')  = '2009'   and t.logintype='0'  group   by  substr(to_char(t.logintime,'YYYY-MM'),6,2)       /**統計一年中每月的登入次數sqlserver */     select  count(*) as sum ,substring(convert(char(7),t.logintime,120),6,2) as month     from  Mwpm_Sys_Loginlog  t   where       convert(char(4),t.logintime,120)  = '2009'   and t.logintype='0'  group   by  substring(convert(char(7),t.logintime,120),6,2)       ///*** 統計一年中各個時間段的登入次數----------oracle文法**/    select to_char(t.logintime,'hh24')||':00-'||to_char(to_number(to_char(t.logintime,'hh24'))+1) ||':00' as internal,count(*) as  sum,    to_char(to_number(to_char(t.logintime,'hh24'))) as time    from  Mwpm_Sys_Loginlog t     where   to_char(t.logintime,'yyyy-MM-dd')='2010-01-18'      group by to_char(t.logintime,'hh24')||':00-'||to_char(to_number(to_char(t.logintime,'hh24'))+1)||':00',to_char(to_number(to_char(t.logintime,'hh24')))           ///*** 統計一年中各個時間段的登入次數----------sqlserver文法**/    select convert(char(2),t.logintime,108)+':00-'+ convert(char(2),dateadd(hh,+1,logintime),108)+':00' as internal,count(*) as sum,    convert(char(2),dateadd(hh,+0,logintime),108) as time     from        Mwpm_Sys_Loginlog t  where     convert(char(10),t.logintime,120)='2010-01-18'       group by convert(char(2),t.logintime,108)+':00-'+ convert(char(2),dateadd(hh,+1,logintime),108)+':00',convert(char(2),dateadd(hh,+0,logintime),108)          /***統計一年中某個月的每天登陸的次數--oracle***/    select  substr(to_char(t.logintime,'YYYY-MM-DD'),9,9) as day ,count(*) as sum  from  Mwpm_Sys_Loginlog  t                       where   to_char(t.logintime,'YYYY-MM')   = '2010-01'                      and t.logintype='0'  group   by   to_char(t.logintime,'YYYY-MM-DD')          /***統計一年中某個月的每天登陸的次數--sqlserver***/    select  substring((convert(char(10),t.logintime,120)),9,2) as day , count(*) as sum    from  Mwpm_Sys_Loginlog  t where  convert(char(7),t.logintime,120) = '2010-01' group by substring(convert(char(10),t.logintime,120),9,2)       /**find()方法不支援  substring((convert(char(10),t.logintime,120)),9,2)**/  

 

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.