在我们写SQL的时候,经常会用到许多内置方法,简化了我们许多代码,也提高了效率,这篇主要总结一些常用的方法。

ISNULL VS COALESCE VS NULLIF

在SQL中,NULL值是比较特殊的,所以如何处理NULL值也是要注意的:

  • NULL + 10 = NULL
  • NULL OR TRUE = NULL
  • NULL OR FALSE = NULL

首先我们看一下这三个方法的定义吧:

Function  Description
ISNULL
ISNULL validates whether an expression is NULL and, if so, replaces the NULL value with an alternate value.
COALESCE
The COALESCE function returns the first non-NULL value from a provided list of expressions.
NULLIF
NULLIF returns a NULL value when the two provided expressions have the same value. Otherwise, the first expression is returned.

那如何在ISNULL和COALESCE中选择呢:

  • ISNULL容易拼写,让人感觉更简洁,直观。但在多链上面的写法很容易让人晕迷,比如:ISNULL(value1, ISNULL(value2, ISNULL(value3, '')))。这个时候推荐使用COALESCE了
  • COALESCE很灵活,而且是ANSI标准SQL的一部分。所以可移值性很好,如果在跨平台上的时候写SQL的时候,推荐COALESCE

Windowing Functions

Function  Description
ROW_NUMBER
ROW_NUMBER returns an incrementing integer for each row within a partition of a set.
ROW_NUMBER will return a unique number within each partition,starting with 1.
RANK
Similar to ROW_NUMBER, RANK increments its value for each row within a
partition of the set. The key difference is that if rows with tied values exist
within the partition, they will receive the same rank value, and the next
value will receive the rank value as if there had been no ties, producing a gap
between assigned numbers.
DENSE_RANK
The difference between DENSE_RANK and RANK is that DENSE_RANK doesn’t
have gaps in the rank values when there are tied values; the next value has
the next rank assignment.
NTILE
NTILE divides the result set into a specified number of groups, based on the
ordering and optional partition clause.

T-SQL Recipes之Common Function的更多相关文章

  1. 在Oracle/SQL Service中通过Function返回Table

    本函数用途:返回一个Table 在Oracle中实现,范例: --在Types中: create or replace type objTable as object ( s_usercode var ...

  2. mybatis的Mapper.xml文件SQL语句BadSqlGrammarException之FUNCTION错误系列

    想必各位在开发过程中一定使用过:统计的功能,用到了很多SQL的函数,于是就直接写在Mapper文件中了: 比如: member_num,MAX(ID) AS newestLoanID,MIN (ID) ...

  3. SQL Stored Procedure and Function

    Anything can be programmable with defined syntax and common lib. )) -- Add the parameters for the st ...

  4. SQL server 自定义函数FUNCTION的使用

    原文链接:https://blog.csdn.net/lanxingbudui/article/details/81736402 前言:        在SQL server中不仅可以可以使用系统自带 ...

  5. use sql trigger call java function

    Use UDF sys_exec to do this. You can use this link to use sys_exec function. It says, sys_exec sys_e ...

  6. sql:Mysql create view,function,procedure

    create database Liber; use Liber; #顯示數据庫 20150210 Geovin Du 涂聚文 SHOW DATABASES; drop table BookKindL ...

  7. sql and csharp: Split Function

    T-SQL: declare @int int,@prov int,@city int,@str nvarchar(500) set @str='天河麗特青春:中國廣東省廣州市天河區天河路623號天河 ...

  8. Yet Another 10 Common Mistakes Java Developers Make When Writing SQL (You Won’t BELIEVE the Last One)--reference

    (Sorry for that click-bait heading. Couldn’t resist ;-) ) We’re on a mission. To teach you SQL. But ...

  9. MySQL This function has none of DETERMINISTIC, NO SQL...错误1418 的原因分析及解决方法

    MySQL开启bin-log后,调用存储过程或者函数以及触发器时,会出现错误号为1418的错误: ERROR 1418 (HY000): This function has none of DETER ...

随机推荐

  1. 第二篇 基于.net搭建热插拔式web框架(沙箱的构建)

    上周五写了一个实现原理篇,在评论中看到有朋友也遇到了我的问题,真的是有种他乡遇知己的感觉,整个系列我一定会坚持写完,并在最后把代码开源到git中.上一篇文章很多人看了以后,都表示不解,觉得不知道我到底 ...

  2. ORACLE_UNQNAME

    之前在自己笔记本win7系统上安装了oracle 11g,当时为了节省资源,没启用EM. 此时查看EM状态: C:\Windows\system32>emctl status dbconsole ...

  3. 手机站使图片高度统一jq代码

    <script> function showImg(){ $(".honor_i_c img").each(function(index, element) { var ...

  4. BZOJ 2096: [Poi2010]Pilots

    Description 求一个最长的序列,最大值最小值之差不超过 \(k\) . Sol 单调队列. 一个队列直接上就行.. Code /******************************* ...

  5. [CodeWars][JS]如何判断给定的数字是否整数

    问题描述: We are asking for a function to take a positive integer value, and return a list of all positi ...

  6. ProgressBar---进度条

    最近在处理标题进度条时,耗费了一些时间,现在总结一下ProgressBar的相关知识,有不对的地方请大神们批评指正! 进度条主要有以下三种: 1.对话框进度条 2.标题进度条 注意:requestWi ...

  7. 文件权限:普通(r、w、x)和特殊(s、t)

     linux中,常常涉及到权限的问题.文件的权限有3组,owner,group,other,每一组都有rwx三种设置.r(4):可读:w(2):可写:x(1):可执行.我们在执行shell命令ls - ...

  8. 使用httpclient 调用selenium webdriver

    结合上次研究的selenium webdriver potocol ,自己写http request调用remote driver代替selenium API selenium web driver ...

  9. Python之Web框架

    Python之Web框架: 一.  Web框架的本质: 对于所有的Web应用,本质上其实就是一个socket服务端,用户的浏览器其实就是一个socket客户端. #!/usr/bin/env pyth ...

  10. Eclipse下载与安装

    有很多小伙伴不知道eclipse在哪里下载以及怎么安装的,那我来给大家做个演示吧. 学习java比较常用的软件就是eclipse,而eclipse安装后并不能马上使用,还需要安装jdk并搭建环境.下面 ...