在正常情况下,所有行的数据不全为空时:
select sum(nvl(1,1)) from dual ;
select nvl(sum(1),1) from dual ;
结果是一样的。
在所有行数据都为空时:
select sum(nvl(null,1)) from dual ;
select nvl(sum(null),1) from dual ;
结果也还是一样。
当查不到数据时:
select sum(nvl(1,1)) from dual where 1<>1;
select nvl(sum(1),1) from dual where 1<>1;
select sum(nvl(null,1)) from dual where 1<>1;
select nvl(sum(null),1) from dual where 1<>1;
可以发现sum在里面和外面结果是不一样的。sum在外面,结果为空,sum在里面结果为1
这是因为sum和nvl的异常处理不一样导致的。
当处理数据时,如果碰到没有数据(注意没有数据不是null)时,sum返回"空",而nvl返回"没有数据"。
所以,当sum在外面时,nvl先返回"没有数据",sum再对"没有数据"做处理,返回结果为"空"。
当sum在里面时,sum先对"没有数据"做处理,返回结果为"空",nvl再对nvl处理,返回结果为1。
分享到:
相关推荐
round((sum(nvl(b.bytes, 0)) - sum(nvl(a.bytes, 0))) / 1024 / 1024, 2) 已经使用M, round(sum(nvl(a.bytes, 0)) / 1024 / 1024, 2) 剩余M, round(sum(c.bytes) / 1021 / 1024, 2) 该用户使用M from ...
NVL(SUM(A.AMOUNT),0) INTO W_ATS_RE_BOOKED_AMT FROM XXXXXXXXX A, ( SELECT B.TRD_NO FROM XXXXXXXXX B WHERE B.CLIENT_CD = W_IN_CLIENT_CD UNION ALL SELECT C.TRD_NO FROM XXXXXXXXX C WHERE...
round(((a.BYTES-nvl(b.BYTES, 0))/a.BYTES)*100,2) "Pct Used", round((1-((a.BYTES-nvl(b.BYTES,0))/a.BYTES))*100,2) "Pct Free" from (select TABLESPACE_NAME, sum(BYTES) BYTES from sys.dba_data_files ...
TRUNC(((b.bytes/1024/1024-(b.bytes/1024/1024-2048)+c.max_extents/1024/1024)-TRUNC((b.bytes-SUM(NVL(a.bytes,0)))/1024/1024))/TRUNC(b.bytes/1024/1024-(b.bytes/1024/1024-2048)+c.max_extents/1024/1024),4...
NVL(a.date, b.date) AS dt1, NVL(a.v1, 0) AS v1, NVL(b.v2, 0) AS v2 FROM a FULL JOIN b ON a.date = b.date ``` **第三题**:计算2019年第一季度(2019-01-01至2019-03-31)用户的利息。这是一个涉及时间...
sum(nvl(a.bytes,0))/(b.bytes)*100 剩余百分比 from dba_free_space a, dba_data_files b where a.file_id = b.file_id group by b.tablespace_name, b.file_name, b.file_id, b.bytes order by b.tablespace_...
(sum(nvl(a.bytes, 0)) / (b.bytes) * 100) AS "Free Percentage" FROM dba_free_space a, dba_data_files b WHERE a.file_id = b.file_id GROUP BY b.tablespace_name, b.file_id, b.bytes ORDER BY b.file_id; `...
a.bytes "Bytes", a.bytes - SUM(nvl(b.bytes, 0)) "Used", SUM(nvl(b.bytes, 0)) "Free", SUM(nvl(b.bytes, 0)) / a.bytes * 100 "%free" FROM dba_data_files a, dba_free_space b WHERE a.file_id = b.file_id...
(B.BYTES - SUM(NVL(A.BYTES, 0))) USED, SUM(NVL(A.BYTES, 0)) FREE, SUM(NVL(A.BYTES, 0)) / (B.BYTES) * 100 PERCENT FROM DBA_FREE_SPACE A, DBA_DATA_FILES B WHERE A.FILE_ID = B.FILE_ID GROUP BY B....
SELECT a.a, a.b, a.c, b.c, b.d, b.f FROM a LEFT OUTER JOIN b ON a.a = b.c; ``` 左外连接是SQL中常用的连接类型之一,用于返回左表的所有记录以及右表中匹配的记录。当右表中没有匹配时,则显示NULL值。 **5. ...
(b.bytes - sum(nvl(a.bytes, 0))) 已使用, sum(nvl(a.bytes, 0)) 剩余空间, sum(nvl(a.bytes, 0)) / (b.bytes) * 100 剩余百分比 from dba_free_space a, dba_data_files b where a.file_id = b.file_id group ...
Y WHERE X.NUM = Y.NUM (+) AND X.INBOUND_QTY + NVL(Y.STOCK_ONHAND,0) <> X.STOCK_ONHAND) B WHERE A.NUM = B.NUM; ``` 这条语句将关联表查询结果显示出来。 八、从数据库中去一年各单位电话费统计 在开发中,...
(b.bytes - SUM(NVL(a.bytes, 0))) AS used_space, SUM(NVL(a.bytes, 0)) AS free_space, SUM(NVL(a.bytes, 0)) / (b.bytes) * 100 AS free_percentage FROM dba_free_space a, dba_data_files b WHERE a.file...
SELECT NVL(col_a, 'NULL') AS col_a, NVL(col_b, DECODE(col_a, NULL, '', 'ALL'||col_a)) AS col_b, SUM(col_c) FROM test_tbl GROUP BY ROLLUP(col_a, col_b); ``` 这个查询不仅给出了基于`col_a`和`col_b`的具体...
SUBSTR((B.BYTES-SUM(NVL(A.BYTES, 0)))/(B.BYTES)*100, 1, 5) AS "Percentage_Free" FROM DBA_FREE_SPACE A, DBA_DATA_FILES B WHERE A.FILE_ID = B.FILE_ID GROUP BY B.TABLESPACE_NAME, B.FILE_NAME, B.BYTES ...
该查询将表`a`的所有记录与表`b`中符合条件的记录进行连接,如果`b`表中不存在匹配项,则结果集中相应位置为空。 #### 三、日程提醒与定时任务 对于需要基于时间触发的应用场景,如日程管理软件中的提醒功能,可以...
- `select b.file_id, b.tablespace_name, b.file_name, b.bytes, (b.bytes-sum(nvl(a.bytes,0))), sum(nvl(a.bytes,0)), sum(nvl(a.bytes,0))/(b.bytes)*100 from dba_free_space a, dba_data_files b where a....
1, 'YYYY/MM')) Y WHERE X.NUM = Y.NUM AND X.INBOUND_QTY + NVL(Y.STOCK_ONHAND, 0) <> X.STOCK_ONHAND) B WHERE A.NUM = B.NUM; ``` #### 解析 这段复杂的SQL语句涉及多个子查询和表之间的关联。主要目的是比较...
ROUND((a.bytes_alloc - NVL(b.bytes_free, 0)) / 1024 / 1024) AS megs_used, ROUND((NVL(b.bytes_free, 0) / a.bytes_alloc) * 100) AS Pct_Free, 100 - ROUND((NVL(b.bytes_free, 0) / a.bytes_alloc) * 100) ...
SUBSTR(((b.bytes - SUM(NVL(a.bytes, 0))) / b.bytes * 100), 1, 5) 利用率 FROM dba_data_files b LEFT JOIN dba_free_space a ON b.tablespace_name = a.tablespace_name GROUP BY b.file_name, b.tablespace...