加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > MsSql教程 > 正文

项目中比较实用的统计查询

发布时间:2020-12-12 15:26:35 所属栏目:MsSql教程 来源:网络整理
导读:情景:假如有一个登陆日志表,表名为sys_loginlog,该表记录的是用户使用系统的情况,有一个字段叫logtime,即登陆时间。问题如下: 1.统计某一年各个月用户登录系统的情况。 2.统计某一个月用户登录系统的情况。 3.统计某一天各个时间段用户登录系统的情况。

情景:假如有一个登陆日志表,表名为sys_loginlog,该表记录的是用户使用系统的情况,有一个字段叫logtime,即登陆时间。问题如下:

1.统计某一年各个月用户登录系统的情况。

2.统计某一个月用户登录系统的情况。

3.统计某一天各个时间段用户登录系统的情况。

答案如下,分别使用了oracle 和 sqlserver语法。

Code:
  1. /**统计一年中每月的登录次数oracl?*/ ??
  2. ?select??count(*)?as?sum?,substr(to_char(t.logintime,'YYYY-MM'),6,2)?as?month??
  3. ??from??Mwpm_Sys_Loginlog??t???where? ??
  4. ??to_char(t.logintime,'YYYY')??=?'2009'???and?t.logintype='0'??group???by??substr(to_char(t.logintime,2) ??
  5. ??
  6. /**统计一年中每月的登录次数sqlserver?*/ ??
  7. ?select??count(*)?as?sum?,substring(convert(char(7),t.logintime,120),2)?as?month??
  8. ??from??Mwpm_Sys_Loginlog??t???where? ??
  9. ??convert(char(4),120)??=?'2009'???and?t.logintype='0'??group???by??substring(convert(char(7),2) ??
  10. ??
  11. ///***?统计一年中各个时间段的登录次数----------oracle语法**/ ??
  12. select?to_char(t.logintime,'hh24')||':00-'||to_char(to_number(to_char(t.logintime,'hh24'))+1)?||':00'?as?internal,count(*)?as??sum,??
  13. to_char(to_number(to_char(t.logintime,'hh24')))?as?time????from??Mwpm_Sys_Loginlog?t? ??
  14. where???to_char(t.logintime,'yyyy-MM-dd')='2010-01-18'? ??
  15. ?group?by?to_char(t.logintime,'hh24'))+1)||':00',to_char(to_number(to_char(t.logintime,'hh24')))? ??
  16. ??
  17. ??
  18. ///***?统计一年中各个时间段的登录次数----------sqlserver语法**/ ??
  19. select?convert(char(2),108)+':00-'+?convert(char(2),dateadd(hh,+1,logintime),108)+':00'?as?internal,count(*)?as?sum,??
  20. convert(char(2),+0,108)?as?time? ??
  21. from?? ??
  22. ??Mwpm_Sys_Loginlog?t??where?????convert(char(10),120)='2010-01-18'? ??
  23. ??group?by?convert(char(2),108)+':00',convert(char(2),108) ??
  24. ??
  25. ??
  26. /***统计一年中某个月的每天登陆的次数--oracle***/ ??
  27. select??substr(to_char(t.logintime,'YYYY-MM-DD'),9,9)?as?day?,count(*)?as?sum??from??Mwpm_Sys_Loginlog??t?? ??
  28. ?????????????????where???to_char(t.logintime,'YYYY-MM')???=?'2010-01'??
  29. ???????????????????and?t.logintype='0'??group???by???to_char(t.logintime,'YYYY-MM-DD') ??
  30. ??
  31. ??
  32. /***统计一年中某个月的每天登陆的次数--sqlserver***/ ??
  33. select??substring((convert(char(10),120)),2)?as?day?,?count(*)?as?sum??
  34. ?from??Mwpm_Sys_Loginlog??t?where??convert(char(7),120)?=?'2010-01'?group?by?substring(convert(char(10),2) ??
  35. ??
  36. /**find()方法不支持??substring((convert(char(10),2)**/??

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读