热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

Oracle中nullsfirst和nullslast在排序中的应用与区别

在Oracle数据库中,`NULLSFIRST`和`NULLSLAST`是`ORDERBY`子句中用于控制空值排序位置的关键字。当使用`NULLSFIRST`时,无论排序顺序是升序(`ASC`)还是降序(`DESC`),包含空值的记录都会被排列在结果集的最前面。相反,`NULLSLAST`则确保空值记录被放置在结果集的最后。这些关键字提供了灵活的排序选项,特别是在处理包含大量空值的数据集时,能够更好地满足不同的业务需求。
Nulls first和nulls last是Oracle Order by支持的语法
如果Order by 中指定了表达式Nulls first则表示null值的记录将排在最前(不管是asc 还是 desc)
如果Order by 中指定了表达式Nulls last则表示null值的记录将排在最后 (不管是asc 还是 desc)
使用语法如下:
--将nulls始终放在最前
select* from baseinfo_operator  order by name  nulls first
--将倒序时合计放在最下面
select case when grouping(clr.cardNo)=1 then '合计' else to_char(clr.cardNo) end cardNo,case when grouping(clr.cardCSN)=1 then to_char(count(*)) || '条' else clr.cardCSN end cardCSN ,
clr.newCardNo as newCardNo,clr.newCardCSN as newCardCSN, cc.nameCN AS cardType,rc.nameCN AS RechargeCenters,rt.nameCN AS RechargeTerminals,o.nameCN AS operatorName,
clr.accountsTransferDate AS accountsTransferDate,clr.customerName AS customerName,clr.papersCard AS papersCard,cc2.nameCN AS transferCardType,
SUM (F2Y (nvl(clr.walletBalances, 0))) AS walletBalances,SUM (nvl(clr.monthTicketNums, 0)) AS monthTicketNums,SUM (F2Y (nvl(clr.monthTicketBalances, 0))) AS monthTicketBalances,
SUM (F2Y (nvl(clr.disCountRechargeMoney,0))) AS disCountRechargeMoney,SUM (nvl(clr.creditScore, 0)) AS creditScore,SUM (nvl(clr.creditNums, 0)) AS creditNums,
SUM (F2Y (nvl(clr.factorage, 0))) AS factorage,sum(F2Y (clr.walletBalances) + F2Y (clr.monthTicketBalances))as sumBalances from CardLossRecords clr
LEFT JOIN CODE_Content cc ON cc.oid = clr.cardType LEFT JOIN BASEINFO_Operator o ON o.oid = clr.operatorOID
 LEFT JOIN CODE_Content cc2 ON cc2.oid = clr.lossType LEFT JOIN BASEINFO_RechargeTerminals rt ON rt.oid = clr.createLossAreaOID
 LEFT JOIN BASEINFO_RechargeCenters rc ON rc.oid = rt.rechargeCenterOID
 where accountsTransfer=1 and isOtherOperate=0 and clr.valid=1  and clr.operatorOID ='0'  and to_char(clr.accountsTransferDate,'yyyy-mm-dd hh24:mi:ss') >= '2017-09-06 00:00:00'
 and to_char(clr.accountsTransferDate,&#39;yyyy-mm-dd hh24:mi:ss&#39;)<&#61;&#39;2017-09-06 23:59:59&#39;
 group by rollup(clr.cardNo,clr.cardCSN,clr.newCardNo,clr.newCardCSN,cc.nameCN,rc.nameCN,rt.nameCN,o.nameCN,clr.accountsTransferDate,clr.customerName,clr.papersCard,cc2.nameCN)
 having( grouping(clr.cardNo)&#61;1 or( grouping(clr.cardNo)&#61;0 and grouping(clr.cardCSN)&#61;0 and grouping(clr.newCardNo)&#61;0 and grouping(clr.newCardCSN)&#61;0
 and grouping(cc.nameCN)&#61;0 and grouping(rc.nameCN)&#61;0 and grouping(rt.nameCN)&#61;0 and grouping(o.nameCN)&#61;0 and grouping(clr.accountsTransferDate)&#61;0
  and grouping(clr.customerName)&#61;0 and grouping(clr.papersCard)&#61;0 and grouping(cc2.nameCN)&#61;0 ))
 
   order by accountsTransferDate desc nulls last

转:https://www.cnblogs.com/shi-yongcui/p/7484520.html



推荐阅读
author-avatar
Fier田野莎莎
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有