关于SQL表字段值缺失的处理办法

时间:2021-08-20 09:06:12

在计算收益率时候,  收益率 = 收益 / 成本

一、如果成本为0,NULL,此时无法计算收益率;

方法:

1.将成本为0的数据 运算

(case when  cost =0 or cost is null then 0  else  income/cost end) as yeild;

select account,sum(case when amount1 is null then 0 else amount1 END) as total,init_date 
from data_stock1 GROUP BY account,init_date;
SELECT
account,
CASE
WHEN min(amount1) <= 0 THEN
'零'
WHEN min(amount1) <= 100 THEN
'十'
WHEN min(amount1) <= 1000 THEN
'百'
ELSE
'百以上'
END AS 级别,
init_date
FROM
data_stock1
GROUP BY
account,
init_date;

2.处理数据为NULL时的运算:

将数据转化为 0; nvl(cost, 0) as cost;

3.四舍五入

round(cost,4)

4.取整

ceil(cost)

floor(cost)

二、计算数据

三、探讨UNION ALL与FULL JOIN ON 运用同一场景的效率问题

[转]http://www.zhixing123.cn/net/27495.html

  一、查询执行最慢的sql

select *
from (select sa.SQL_TEXT,
sa.SQL_FULLTEXT,
sa.EXECUTIONS "执行次数",
round(sa.ELAPSED_TIME / 1000000, 2) "总执行时间",
round(sa.ELAPSED_TIME / 1000000 / sa.EXECUTIONS, 2) "平均执行时间",
sa.COMMAND_TYPE,
sa.PARSING_USER_ID "用户ID",
u.username "用户名",
sa.HASH_VALUE
from v$sqlarea sa
left join all_users u
on sa.PARSING_USER_ID = u.user_id
where sa.EXECUTIONS > 0
order by (sa.ELAPSED_TIME / sa.EXECUTIONS) desc)
where rownum <= 50;

  二、查询次数最多的 sql

select *
from (select s.SQL_TEXT,
s.EXECUTIONS "执行次数",
s.PARSING_USER_ID "用户名",
rank() over(order by EXECUTIONS desc) EXEC_RANK
from v$sql s
left join all_users u
on u.USER_ID = s.PARSING_USER_ID) t
where exec_rank <= 100;

四、关于两表出现 null值的字段加减