T-SQL Recipes之Common Function
在我们写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 apartition of the set. The key difference is that if rows with tied values existwithin the partition, they will receive the same rank value, and the nextvalue will receive the rank value as if there had been no ties, producing a gapbetween assigned numbers. |
||||||||||
DENSE_RANK |
The difference between DENSE_RANK and RANK is that DENSE_RANK doesn’thave gaps in the rank values when there are tied values; the next value hasthe next rank assignment. |
||||||||||
NTILE |
NTILE divides the result set into a specified number of groups, based on the
|
||||||||||
T-SQL Recipes之Common Function的更多相关文章
- 在Oracle/SQL Service中通过Function返回Table
本函数用途:返回一个Table 在Oracle中实现,范例: --在Types中: create or replace type objTable as object ( s_usercode var ...
- mybatis的Mapper.xml文件SQL语句BadSqlGrammarException之FUNCTION错误系列
想必各位在开发过程中一定使用过:统计的功能,用到了很多SQL的函数,于是就直接写在Mapper文件中了: 比如: member_num,MAX(ID) AS newestLoanID,MIN (ID) ...
- SQL Stored Procedure and Function
Anything can be programmable with defined syntax and common lib. )) -- Add the parameters for the st ...
- SQL server 自定义函数FUNCTION的使用
原文链接:https://blog.csdn.net/lanxingbudui/article/details/81736402 前言: 在SQL server中不仅可以可以使用系统自带 ...
- 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 ...
- sql:Mysql create view,function,procedure
create database Liber; use Liber; #顯示數据庫 20150210 Geovin Du 涂聚文 SHOW DATABASES; drop table BookKindL ...
- sql and csharp: Split Function
T-SQL: declare @int int,@prov int,@city int,@str nvarchar(500) set @str='天河麗特青春:中國廣東省廣州市天河區天河路623號天河 ...
- 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 ...
- MySQL This function has none of DETERMINISTIC, NO SQL...错误1418 的原因分析及解决方法
MySQL开启bin-log后,调用存储过程或者函数以及触发器时,会出现错误号为1418的错误: ERROR 1418 (HY000): This function has none of DETER ...
随机推荐
- angularjs 中的setTimeout(),setInterval() / $interval 和 $timeout
$interval window.setInterval的Angular包装形式.Fn是每次延迟时间后被执行的函数. 间隔函数的返回值是一个承诺.这个承诺将在每个间隔刻度被通知,并且到达规定迭代次数后 ...
- bzoj2928: [Poi1999]飞弹
惨啊…… 被卡常是一种什么感受&…… 很明显的分治. 我们首先可以找到所有点中的最低点,然后对所有点进行一次极角排序,选取一个点使得他各侧飞弹和地堡一样多,并对两侧继续进行分治. 很容易证明这 ...
- tmux常用快捷键
首先输入命令开始. $ tmux 然后,基本组合 Ctrl+b(同时按下ctrl和b),按完松开 松开后,按%(即同时按下shift和5),是水平分割 按“(即同时按shift和'),是纵向分割 按上 ...
- 【openStack】Libcloud 如何支持 keystone V3?
Examples This section includes some examples which show how to use the newly available functionality ...
- 【ASP.NET】利用Nuget打包package——命令行方式
通过命令行 官方说明,可以参考:creating-and-publishing-a-package 如果你希望可以使用图形界面的方式,请参考这篇文章 打包dll 使用如下的命令: nuget ...
- c语言内存原理
1.内存寻址由大到小,优先分配内存地址比较大得字节给变量 2.变量越先定义,内存地址就越大 3.取得变量的地址:&变量名 4.输出地址 %p
- printf和scanf
一 printf和scanf(printf和scanf是c语言中的输入输出库函数) printf(”格式控制“,输出列表) <1>格式控制 其中包含 格式声明(%+相应格式对应字母).转义 ...
- PDF虚拟打印机
1.安装福昕PDF阅读器,自己就会安装上PDF虚拟打印机 2.通过office2010之后版本中自带的另存为功能转换为pdf 3.pdf虚拟打印机工具:工具1:http://www.win2pdf.c ...
- NFC OMA 访问
正常的OMA访问: 但基本上IC原厂都会提供NFC swp-sim这一块的访问,但关于NFC内部eSe的访问一般NFC服务商又会整一套出来,导致有两套独立的SmartcardSevice需要运行,但他 ...
- ALS
最近看了一些关于ALS(肌萎缩性脊髓侧索硬化症)的电视剧和一本ALS患者的生活自述的书. 一次偶然的机会在一部日剧<我所存在的时间>中看到了ALS这种疾病,感觉这就像众病之王--癌症一样, ...