在我们写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. Sality.m分析

    Sality.m分析 0x1.样本概述 FILE_A MD5:1C9A0E01C6033801AFC5A12DE1CC5BDC FILE_B MD5:4B6B70F4A199CF3EAC1554B08 ...

  2. OSI7层模型详解

    首先我们借用百度百科上的图片来基本了解一下OSI7层模型的名称以及结构.下面我将从最底层一层一层往上介绍. 物理层:基于Bit传输,是属于物理信道,最基本的机械.电子.定时接口通信信道. 数据链路层: ...

  3. java基础杂烩

    1. int ... arg: 可变参数   2. 数组拷贝放在JDK中的System类中,arraycopy(原数组,从原数组那一个索引开始拷贝,目标数组,在目标数组哪一个位置开始粘贴,拷贝元素的个 ...

  4. (转)CString截取字符串全攻略

    附:去掉空格方法 CString str("e w r we  中华ADF 华AG中"); newstr=str.Replace(" ", "&quo ...

  5. PostgreSQL 允许远程访问设置方法

     安装PostgreSQL数据库之后,默认是只接受本地访问连接.如果想在其他主机上访问PostgreSQL数据库服务器,就需要进行相应的配置. 配置远程连接PostgreSQL数据库的步骤很简单,只需 ...

  6. sh6.脚本磁盘分区格式化

    练习1. 写一个脚本,通过ping 命令测试192.168.0.100到192.168.0.254之间的所有主机是否在线, 如果在线,就显示"ip is up."IP为真实IP地址 ...

  7. shell 指定范围产生随机数

    #/bin/bash echo "---------------产生随机数---------------" read -p "请输入起始数:" a read - ...

  8. SSM框架总结(1)

    ----------------------20170106-------------------------------------- 1.mabatis.genertor中自动生成文件,需要在ge ...

  9. WPF 如何绘制不规则按钮,并且有效点击范围也是不规则的

    最近在做一个东西,如地图,点击地图上的某一区域,这一区域需要填充成其他颜色.区域是不规则的,而且点击该区域的任一点,都能够变色.普通的按钮只是简单的加载一幅图肯定是不行的.查了很多资料,终于把它搞定了 ...

  10. angular1.x的简单介绍 (一)

    angular1.x作为经典的mvc框架,可以创建能够复用的组件,也可进行双向数据绑定.国内的vue.js/avaloon.js都是同类型的框架.适合使用angularjs的项目有大型信息化管理系统: ...