SQL笔记

统计排名

select A.deptcode,A.score,RANK() over(order by A.score desc) as ran from (select deptcode,sum(score) as score from tb_kaohe group by deptcode )A

在这里插入图片描述

查询年份

今年 year(now())
去年 year(dateadd(yy,-1,getdate()))
前年 year(dateadd(yy,-2,getdate()))

查询12个月份数据(列转行)

select concat
	(
		jan, ',', Feb, ',', Mar, ',', Apr, ',', May, ',', June, ',', July, ',', Aug, ',', Sept, ',', Oct,',', Nov, ',', Dece
	) as Year
from 
	(
		select sum(case month (sendtime) when '1'  then 1 else 0 end) as jan,
               sum(case month (sendtime) when '2'  then 1 else 0 end) as Feb,
               sum(case month (sendtime) when '3'  then 1 else 0 end) as Mar,
               sum(case month (sendtime) when '4'  then 1 else 0 end) as Apr,
               sum(case month (sendtime) when '5'  then 1 else 0 end) as May,
               sum(case month (sendtime) when '6'  then 1 else 0 end) as June,
               sum(case month (sendtime) when '7'  then 1 else 0 end) as July,
               sum(case month (sendtime) when '8'  then 1 else 0 end) as Aug,
               sum(case month (sendtime) when '9'  then 1 else 0 end) as Sept,
               sum(case month (sendtime) when '10' then 1 else 0 end) as Oct,
               sum(case month (sendtime) when '11' then 1 else 0 end) as Nov,
               sum(case month (sendtime) when '12' then 1 else 0 end) as Dece
        from "DOCUMENTFILE"."tb_sendtelegram"
        where year (sendtime)= YEAR (NOW())
    )

统计各市(dept_id)下单位的收发文数量

select t.DEPT_ID PARENT_ID, count(s.senduserorg) num
from (
			select distinct DEPT_ID
      		from "FILETRANSFER"."sys_dept"
     		 where DEPT_ID BETWEEN '103' and '115'
     ) t
left join
     (
      	select distinct PARENT_ID, DEPT_ID
      	from "FILETRANSFER"."sys_dept"
      	where PARENT_ID BETWEEN 103 and 115
     ) a
on t.DEPT_ID = a.PARENT_ID
left join
     (
        select * from "FILETRANSFER"."tb_sendtelegram" where year (sendtime)= YEAR (NOW())
     ) s
on a.DEPT_ID = s.senduserorg
group by t.DEPT_ID;

自动生成 ID

DM
select sys_guid() from dual
<selectKey keyProperty="id" resultType="java.lang.String" order="BEFORE">
	select sys_guid() from dual
</selectKey>
MySQL
select replace(uuid(),'-','') from dual
<selectKey keyProperty="id" resultType="java.lang.String" order="BEFORE">
	select replace(uuid(),'-','') from dual
</selectKey>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值