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 ...
随机推荐
- -webkit-box-flex被内容撑开了
<!doctype html> <html> <head> <meta charset="utf-8"> <title> ...
- 关于JavaScript初级的知识点一(持续更新 )
自己刚开始接触JS这是自己一个多月以来的一些总结和回顾. 一.什么是js? js是一种弱类型的脚本语言,是HTML的3大组成部分之一.HTML标签 CSS样式 JS脚本. 二.js的5种基本数据类型 ...
- Visual Studio 默认保存为UTF8编码
Visual Studio (中文版)默认保存的文本文件是GB2312编码(代码页936)的,默认的行尾(End of line)是CRLF的. 如果仅仅是在windows下开发问题也不大,但是涉及到 ...
- 【转】Inode详解
Inode详解 转自: Inode详解 一.inode是什么 理解inode,要从文件储存说起. 文件储存在硬盘上,硬盘的最小存储单位叫做"扇区"(Sector).每个扇区储存 ...
- SDN/NFV运营商商业化部署
三大运营商发布未来网络架构,并逐步加快SDN/NFV商业化部署的步伐.中国联通发布其新一代网络架构<CUBE-Net 2.0白皮书>,并与20多家合作伙伴共同启动了“新一代网络”合作研发计 ...
- Ubuntu下安装MySQL-python教程
http://blog.csdn.net/liushuaikobe/article/details/8586224 写得非常的好,也很详细. 有密码:mysql -u root -p 无密码:mysq ...
- Git 常用操作和问题解决
记录一下自己用git作为项目管理过程中常见的错误以及处理方法 1.git pull 出现问题 git pull出现的问题多为远程分支文件和本地冲突 错误提示:error: Your local cha ...
- POJ 2533 Longest Ordered Subsequence LCS O(n*log(n))
题目链接 最长上升子序列O(n*log(n))的做法,只能用于求长度不能求序列. #include <iostream> #define SIZE 1001 using namespace ...
- Web应用性能优化思路
瓶颈是什么? 一条4车道的公路,运行非常顺畅,突然出了点事故,事故车导致某个地方只剩下1车道,然后就开始堵车,因为四辆车同时塞向一个车道里.把这个事故清除了,故障车拖走了,道路会开始恢复了通畅. 这个 ...
- 【用xocde5打包 在IOS7以下也能显示无默认gloss 效果 图解】
图解,只需要这几个地方修改以下就好了,看不懂的话,请留言 完毕:记着clean 以下项目文件