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 ...
随机推荐
- 本地数据Store。Cookie,Session,Cache的理解。Timer类主要用于定时性、周期性任务 的触发。刷新Store,Panel
本地数据Store var monthStore = Ext.create('Ext.data.Store', { storeId : 'monthStore', autoLoad : false, ...
- How to use Bundle&Minifier and bundleconfig.json in ASP.NET Core
引言 我们在ASP.NET MVC 中经常会用到 bundleConfig.cs 文件来进行我们 css 和 js 的绑定, 那么在ASP.NET Core 中我们应该如何使用呢? 步骤一 在 Vis ...
- JavaScript 日期选择器 Pikaday
找一些插件的过程实在太痛苦了...好容易找到一个,赶紧记录下.免得以后重复浪费时间在这上面. 插件名:Pikaday github地址:https://github.com/dbushell/Pika ...
- c/c++中关于sizeof、strlen的使用说明
sizeof: 一般指类型.变量等占用的内存大小(由于在编译时计算,因此sizeof不能用来返回动态分配的内存空间的大小) strlen: c字符串的长度(参数必须是字符型指针 char*,当数组名作 ...
- PHP 数据库连接工具类(MySQLI函数包装)
====================mysql===================== <?php class mysql { private $mysqli; private $resu ...
- [CodeWars][JS]实现链式加法
在知乎上看到这样一个问题:http://www.zhihu.com/question/31805304; 简单地说就是实现这样一个add函数: add(x1)(x2)(x3)...(xn) == x1 ...
- CentOS添加用户并加入sudo权限
# 新增用户 useradd username # 设置密码 passwd username # 加入sudo ## 打开sudo配置文件 visudo ## 找到下面这两行,并在下面新增红色部分 # ...
- Apple Pay 初探
Apple Pay 一.概述 1.支付方式:Touch ID/ Passcode 2.设备要求:iPhone6以上(iphone:线上/线下 ipad:线上 watch:线下) 3.系统要求:iOS8 ...
- tp5 model 中的查询范围(scope)
查询范围scope在model中定义,在controller中使用 namespace app\index\model; use think\Model; class User extends Mod ...
- November 2nd Week 45th Wednesday 2016
If your ship doesn't come in, swim out to it. 如果你的船不驶进来,那你就朝他游过去吧! Swim out to it, don't fear that y ...