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 ...
随机推荐
- 部分Android手机拍照后照片被旋转的解决方案
在部分Android手机(如MT788.Note2)上,使用Camera拍照以后,得到的照片会被自动旋转(90°.180°.270°),这个情况很不符合预期.仔细分析了一下,因为照片属性中是存储了 ...
- 排序算法总结第二弹----冒泡排序---javascript描述
上篇博文总结了选择排序,这篇来看冒泡排序,接上篇. 冒泡排序思想:若是正再将一组数据升序排序, 第一趟:比较相邻的数据,当左侧值大于右侧值将他们进行交换,将较小值向前浮动,大值向后冒泡,直至比较到最后 ...
- iOS如何彻底避免数组越界
我们先来看看有可能会出现的数组越界Crash的地方: ? 1 2 3 4 5 6 7 - (void)tableView:(UITableView *)tableView didSelectRowAt ...
- BZOJ 4668: 冷战
Description 在一个图上,在两个点间连一条边,问这两个点最早在什么时候联通. Sol 并查集+启发式合并. 按秩合并的并查集...我也不知道什么是按秩合并,反正就跟启发式合并差不多,合并的时 ...
- ubuntu 12.04 install docker-engine1.12.3
root@node3:/data/src# cat /etc/issueUbuntu 12.04.4 LTS \n \l root@node3:/data/src# cat /etc/apt/so ...
- 遍历list、set、map和array
public static void main(String[] args) { /*1. List*/ ArrayList<Integer> list = new ArrayList&l ...
- Quartz定时任务简单实例
文章纲要: 初步搭建一个由Quartz为引擎集群的定时任务模块,功能为每隔30秒打印一条信息(Hello World!!!) 一.环境 Spring MVC Mevan Quartz 2.2.1 二. ...
- maven学习(下)利用Profile构建不同环境的部署包
接上回继续,项目开发好以后,通常要在多个环境部署,象我们公司多达5种环境:本机环境(local).(开发小组内自测的)开发环境(dev).(提供给测试团队的)测试环境(test).预发布环境(pre) ...
- 结合实例详细介绍encodeURI()、encodeURIComponent()、decodeURI()、decodeURIComponent()使用方法
在介绍encodeURI().encodeURIComponent().decodeURI().decodeURIComponent()方法前我们需要了解Global对象的概念: Global(全 ...
- 使用 jsoup 对 HTML 文档进行解析和操作
jsoup 简介 Java 程序在解析 HTML 文档时,相信大家都接触过 htmlparser 这个开源项目,我曾经在 IBM DW 上发表过两篇关于 htmlparser 的文章,分别是:从 HT ...