SQL统计语句记录

1.达梦数据库

统计指定单位的12个月份的业务数据

sql 复制代码
SELECT 
      a.DEPT_ID, b.dept_name, a.USER_NAME, count(a.dept_id) as count, 
      sum(case when  to_char(a.CREATE_TIME,'yyyy-mm') = '2023-01' THEN '1' else '0' end) as one,
      sum(case when  to_char(a.CREATE_TIME,'yyyy-mm') = '2023-02' THEN '1' else '0' end) as two,
      sum(case when  to_char(a.CREATE_TIME,'yyyy-mm') = '2023-03' THEN '1' else '0' end) as three,
      sum(case when  to_char(a.CREATE_TIME,'yyyy-mm') = '2023-04' THEN '1' else '0' end) as four,
      sum(case when  to_char(a.CREATE_TIME,'yyyy-mm') = '2023-05' THEN '1' else '0' end) as five,
      sum(case when  to_char(a.CREATE_TIME,'yyyy-mm') = '2023-06' THEN '1' else '0' end) as six,
      sum(case when  to_char(a.CREATE_TIME,'yyyy-mm') = '2023-07' THEN '1' else '0' end) as seven,
      sum(case when  to_char(a.CREATE_TIME,'yyyy-mm') = '2023-08' THEN '1' else '0' end) as eight,
      sum(case when  to_char(a.CREATE_TIME,'yyyy-mm') = '2023-09' THEN '1' else '0' end) as nine,
      sum(case when  to_char(a.CREATE_TIME,'yyyy-mm') = '2023-10' THEN '1' else '0' end) as ten,
      sum(case when  to_char(a.CREATE_TIME,'yyyy-mm') = '2023-11' THEN '1' else '0' end) as eleven,
      sum(case when  to_char(a.CREATE_TIME,'yyyy-mm') = '2023-12' THEN '1' else '0' end) as twelve
FROM 
    SYS_USER  as a
    left join sys_dept as b
     on a.dept_id = b.dept_id
WHERE 
     a.CREATE_TIME >='2023-01-01' AND a.CREATE_TIME <= '2023-12-31' 
     group by a.dept_id, b.dept_name,  user_name
相关推荐
苹果酱05671 小时前
「Mysql优化大师一」mysql服务性能剖析工具
java·vue.js·spring boot·mysql·课程设计
Minxinbb1 小时前
MySQL中Performance Schema库的详解(上)
数据库·mysql·dba
滚雪球~1 小时前
2002 - Can‘t connect to server on ‘192.168.1.XX‘ (36)
mysql·navicat
mmsx2 小时前
android sqlite 数据库简单封装示例(java)
android·java·数据库
zpjing~.~3 小时前
Mongo 分页判断是否有下一页
数据库
2401_857600953 小时前
技术与教育的融合:构建现代成绩管理系统
数据库·oracle
秋恬意4 小时前
Mybatis能执行一对一、一对多的关联查询吗?都有哪些实现方式,以及它们之间的区别
java·数据库·mybatis
潇湘秦4 小时前
一文了解Oracle数据库如何连接(1)
数据库·oracle
雅冰石4 小时前
oracle怎样使用logmnr恢复误删除的数据
数据库·oracle
web前端神器4 小时前
mongodb给不同的库设置不同的密码进行连接
数据库·mongodb