数据库查询运维sql笔记记录
(更新中)
比较两个表中数据的差异性:
select sum(nb),zd1,zd2,zd3 from (
select '1' as nb ,zd1,zd2,zd3 from tb1
union all
select '-1' as nb ,zd1,zd2,zd3 from tb1_bak
) group by zd1,zd2,zd3
having sum(nb) <>0 order by zd1,nb
找出一个账号对应多个客户的情况
select ckzh,khh,count(*)
from ( select ckzh,khh,count(*)
from sds_grck group by ckzh,khh having count(*)>1
)t1 group by ckz having count(*)>1
日期函数(impala)
某日期下的前一天:
select date_add(cast('2020-02-29' as timestamp),1);
后一天:
select date_add(cast('2020-02-29' as timestamp),-1);
日期天数:
select datediff(cast('2020-03-31' as timestamp),cast('2020-02-29' as timestamp)) --31 (不在日期范围内的相减会置为空)
日期取一个月
select add_months(cast('2020-02-29' as timestamp),1) --2020-03-31
取整函数
向下取整:
select floor(3.89) --3
select round(3.89) --4