当前位置:首页 » 编程语言 » sqlcoalesce函数
扩展阅读
webinf下怎么引入js 2023-08-31 21:54:13
堡垒机怎么打开web 2023-08-31 21:54:11

sqlcoalesce函数

发布时间: 2022-01-22 02:03:34

1. 求SQL语言中COALESCE字符函数的用法

功能:返回其参数中的第一个非空表达式,当你要在n个字段中选取某一个非空值
可以用它,比如下面语句
select Coalesce(null,null,1,2,null)union
select Coalesce(null,11,12,13,null)union
select Coalesce(111,112,113,114,null)

返回结果:
1
11
111

2. 如何使用Oracle的COALESCE函数

数据库应用软件很多时候将多重的、相关的实体信息保存在同一个表格中。例如,购买的零件和本地生产的零件都是零件,经理和工人都是员工,尽管多重的实体类型在数据存储上略有不同,但是它们有太多的相同之处,因此通常使用一个表格而不是两个。
处理这样的表格通常意味着对每一行进行条件测试来检查哪个实体类型正在被处理,然后返回每种类型的不同结果集。CASE语句可以用来完成这一工作。
从Oracle 9i版开始,COALESCE函数在很多情况下就成为替代CASE语句的一条捷径,COALESCE的格式如下:
COALESCE (expression_1, expression_2, ...,expression_n)
列表中第一个非空的表达式是函数的返回值,如果所有的表达式都是空值,最终将返回一个空值。
使用COALESCE的秘密在于大部分包含空值的表达式最终将返回空值(连接操作符||是一个值得注意的例外)。例如,空值加任何值都是空值,空值乘任何值也都是空值,依此类推。
这样您就可以构建一系列表达式,每个表达式的结果是空值或非空,这就像一个CASE语句,表达式按照顺序进行测试,第一个满足条件的值就确定了结果。
列表A展示了名为PARTS的表格的一部分,该表格存储了购买的零件数据和生产的零件数据,如果是购买的零件,那么part_type列的值为‘P’,如果是本地生产或组装的则是‘B’;此外,对于购买的零件,purchase_cost 列会显示购买成本,而本地生产的零件则是空值;而且,本地生产的零件还有material_qty和material_cost两列的信息,对于购买的零件则是空值。
您可以使用一个CASE语句来测试part_type列的值并返回either purchase_cost和material_qty列与material_cost列的乘积;不过COALESCE可以用一行语句解决这个问题:
COALESCE(purchase_cost, material_qty * material_cost)
如果数据行中存储的是一个购买的零件,那么purchase_cost就不是空值,将返回purchase_cost的值;然而,对于本地生产的零件,purchase_cost是空值,COALESCE会忽略它,然后将material_qty和material_cost相乘,并将乘积作为结果返回。
SELECT part_id "Part", part_type "Type",
COALESCE(purchase_cost, material_qty * material_cost) "Cost"
FROM parts;
您可以对任何数量的表达式重复使用这个模式,COALESCE是一个非常便捷的方法对统一表格中的多重实体求值。
最后,还要说一点CASE语句的优点,就是CASE是自动进行文档记录的,这便于理解和解读正在发生的事情。

3. SQL中 nvl()coalesce()decode()这三个函数是不是递进包含关系

nvl(bonus,0) 意思是 如果 bonus is null , 那么返回 0, 否则返回 bonus

coalesce(bonus,0,1) 意思是 返回 参数列表中, 第一个非 空的数据。
也就是相当于, 如果 bonus is null , 那么返回 0, 否则返回 bonus。
这里的最后一个参数 1, 目测是打酱油的。

coalesce(bonus,null,0) 意思是 返回 参数列表中, 第一个非 空的数据。
也就是相当于, 如果 bonus is null , 那么第2个参数还是 null, 最后返回第3个参数 0

decode(name,‘apple’,0) 意思是, 如果 name = 'apple' 那么返回 0
否则的话 , 就是返回 null 了。

4. SQL Server ISNULL函数和Coalesce函数替换空值的区别

SELECT COALESCE('',0)结果0
SELECT COALESCE(' ',0) 结果0
SELECT COALESCE(null,0) 结果0
SELECT COALESCE(123,0) 结果123
SELECT ISNULL('',0) 结果''
SELECT ISNULL(null,0) 结果0
SELECT ISNULL(123,0)结果123
由结果结果可以看出COALESCE函数对于空值处理和NULL值都起作用。

5. 求SQL的全部函数!完整的加100分!

一.聚合函数
AVG 返回组中值的平均值。空值将被忽略
BINARY_CHECKSUM 返回对表中的行或表达式列表计算的二进制校验值。BINARY_CHECKSUM 可用于检测表中行的更改
CHECKSUM 返回在表的行上或在表达式列表上计算的校验值。CHECKSUM 用于生成哈希索引
CHECKSUM_AGG 返回组中值的校验值。空值将被忽略
COUNT 返回组中项目的数量
COUNT_BIG 返回组中项目的数量。COUNT_BIG 的使用与 COUNT 函数相似。它们之间的唯一差别是它们的返回值:COUNT_BIG 总是返回 bigint 数据类型值,而 COUNT 则总是返回 int 数据类型值
GROUPING "是一个聚合函数,它产生一个附加的列,当用 CUBE 或 ROLLUP 运算符添加行时,附加的列输出值为1,当所添加的行不是由 CUBE 或 ROLLUP 产生时,附加列值为0。
仅在与包含 CUBE 或 ROLLUP 运算符的 GROUP BY 子句相联系的选择列表中才允许分组"
MAX 返回表达式的最大值
MIN 返回表达式的最小值
SUM 返回表达式中所有值的和,或只返回 DISTINCT 值。SUM 只能用于数字列。空值将被忽略
STDEV 返回给定表达式中所有值的统计标准偏差
STDEVP 返回给定表达式中所有值的填充统计标准偏差
VAR 返回给定表达式中所有值的统计方差。
VARP 返回给定表达式中所有值的填充的统计方差。

二.数学函数
ABS 返回给定数字表达式的绝对值
ACOS 返回以弧度表示的角度值,该角度值的余弦为给定的 float 表达式;本函数亦称反余弦。
ASIN 返回以弧度表示的角度值,该角度值的正弦为给定的 float 表达式;亦称反正弦
ATAN 返回以弧度表示的角度值,该角度值的正切为给定的 float 表达式;亦称反正切
ATN2 返回以弧度表示的角度值,该角度值的正切介于两个给定的 float 表达式之间;亦称反正切
CEILING 返回大于或等于所给数字表达式的最小整数
COS 一个数学函数,返回给定表达式中给定角度(以弧度为单位)的三角余弦值
COT 一个数学函数,返回给定 float 表达式中指定角度(以弧度为单位)的三角余切值
DEGREES 当给出以弧度为单位的角度时,返回相应的以度数为单位的角度
EXP 返回所给的 float 表达式的指数值
FLOOR 返回小于或等于所给数字表达式的最大整数
LOG 返回给定 float 表达式的自然对数
LOG10 返回给定 float 表达式的以 10 为底的对数
PI 返回 PI 的常量值
POWER 返回给定表达式乘指定次方的值
RADIANS 对于在数字表达式中输入的度数值返回弧度值
RAND 返回 0 到1 之间的随机float 值
ROUND 返回数字表达式并四舍五入为指定的长度或精度
SIGN 返回给定表达式的正 (+1)、零 (0) 或负 (-1) 号
SIN 以近似数字 (float) 表达式返回给定角度(以弧度为单位)的三角正弦值
SQUARE 返回给定表达式的平方
SQRT 返回给定表达式的平方根
TAN 返回输入表达式的正切值
三.日期函数
DATEADD 在向指定日期加上一段时间的基础上,返回新的 datetime 值。
DATEDIFF 返回跨两个指定日期的日期和时间边界数
DATENAME 返回代表指定日期的指定日期部分的字符串
DATEPART 返回代表指定日期的指定日期部分的整数
DAY 返回代表指定日期的天的日期部分的整数
GETDATE 按 datetime 值的 Microsoft�0�3 SQL Server�6�4 标准内部格式返回当前系统日期和时间
GETUTCDATE 返回表示当前 UTC 时间(世界时间坐标或格林尼治标准时间)的 datetime 值
MONTH 返回代表指定日期月份的整数
YEAR 返回表示指定日期中的年份的整数
四.系统函数
APP_NAME 返回当前会话的应用程序名称(如果应用程序进行了设置)。
CASE 表达式 计算条件列表并返回多个可能结果表达式之一(详见PPT资料)
CAST 和 CONVERT 将某种数据类型的表达式显式转换为另一种数据类型(详见PPT资料)
COALESCE 返回其参数中第一个非空表达式
COLLATIONPROPERTY 返回给定排序规则的属性
CURRENT_TIMESTAMP 返回当前的日期和时间。等价于 GETDATE()
CURRENT_USER 返回当前的用户。价于 USER_NAME()

6. COALESCE(MAX(meta_id),0)+1,请问这个SQL语句是什么意思

表达式:COALESCE(MAX(meta_id),0)+1
其意思为选取字段"meta_id"的最大值+1,如果该最大值为Null(空值),则将空值替换为0,然后+1

COALESCE函数简要说明:
COALESCE (expression_1, expression_2, ...,expression_n)依次参考各参数表达式,遇到非null值即停止并返回该值。如果所有的表达式都是空值,最终将返回一个空值。

7. 如何使用Oracle的COALESCE函数和nvl函数

NVL是Oracle PL/SQL中的一个函数。它的格式是NVL( string1, replace_with)。它的功能是如果string1为NULL,则NVL函数返回replace_with的值,否则返回string1的值,如果两个参数都为NULL ,则返回NULL。

注意事项:string1和replace_with必须为同一数据类型,除非显式的使用TO_CHAR函数进行类型转换。
例:NVL(TO_CHAR(numeric_column), 'some string') 其中numeric_column代指某个数字类型的值。
例:nvl(yanlei777,0) > 0
NVL(yanlei777, 0) 的意思是 如果 yanlei777 是NULL, 则取 0值

8. 函数VALUE和COALESCE到底有没有区别

Purpose

NVL lets you replace null (returned as a blank) with a string in the results of a query. If expr1 is null, then NVL returns expr2. If expr1 is not null, then NVLreturns expr1.

The arguments expr1 and expr2 can have any data type. If their data types are different, then Oracle Database implicitly converts one to the other. If they cannot be converted implicitly, then the database returns an error. The implicit conversion is implemented as follows:

• If expr1 is character data, then Oracle Database converts expr2 to the data type of expr1 before comparing them and returns VARCHAR2 in the character set of expr1.

• If expr1 is numeric, then Oracle Database determines which argument has the highest numeric precedence, implicitly converts the other argument to that data type, and returns that data type.

Examples

SQL> select * from scott.procts;

LIST_PRICE MIN_PRICE

---------- ----------

10000 8000

20000

30000 30000

SQL> select min_price,nvl(min_price,0) from scott.procts;

MIN_PRICE NVL(MIN_PRICE,0)

---------- ----------------

8000 8000

0

30000 30000

---------------------------------------------------------------------------------------------------------------------------------------

nvl2用法为nvl2(expr1,expr2,expr3),其作用是判断expr1是否为null,若不为null,返回expr2,为空返回expr3。

nvl(expr1,expr2)等同于nvl2(expr1,expr1,expr2)。

官方文档用法解释如下:

NVL2

Syntax

De.ion of nvl2.gif follows

Purpose

NVL2 lets you determine the value returned by a query based on whether a specified expression is null or not null. If expr1 is not null, then NVL2 returnsexpr2. If expr1 is null, then NVL2 returns expr3.

The argument expr1 can have any data type. The arguments expr2 and expr3 can have any data types except LONG.

If the data types of expr2 and expr3 are different, then Oracle Database implicitly converts one to the other. If they cannot be converted implicitly, then the database returns an error. If expr2 is character or numeric data, then the implicit conversion is implemented as follows:

• If expr2 is character data, then Oracle Database converts expr3 to the data type of expr2 before returning a value unless expr3 is a null constant. In that case, a data type conversion is not necessary, and the database returns VARCHAR2 in the character set of expr2.



If expr2 is numeric data, then Oracle Database determines which argument has the highest numeric precedence, implicitly converts the other argument to that data type, and returns that data type.

Examples

SQL> select list_price,www.hbbz08.com min_price,nvl2(min_price,min_price,list_price) nv2_m from scott.procts;

LIST_PRICE MIN_PRICE NV2_M

---------- ---------- ----------

10000 8000 8000

20000 20000

30000 30000 30000

---------------------------------------------------------------------------------------------------------------------------------------

nullif用法为nullif(expr1,expr2),其作用是判断expr1与expr2是否相等,若相等则返回null,否则返回expr1。

官方文档用法解释如下:

NULLIF

Syntax

De.ion of nullif.gif follows

Purpose

NULLIF compares expr1 and expr2. If they are equal, then the function returns null. If they are not equal, then the function returns expr1. You cannot specify the literal NULL for expr1.

If both arguments are numeric data types, then Oracle Database determines the argument with the higher numeric precedence, implicitly converts the other argument to that data type, and returns that data type. If the arguments are not numeric, then they must be of the same data type, or Oracle returns an error.

The NULLIF function is logically equivalent to the following CASE expression:
CASE WHEN expr1 = expr2 THEN NULL ELSE expr1 END

Examples

SQL> select list_price,min_price,nullif(list_price,min_price) from scott.procts;

LIST_PRICE MIN_PRICE NULLIF(LIST_PRICE,MIN_PRICE)

---------- ---------- ----------------------------

10000 8000 10000

20000 20000

30000 30000

---------------------------------------------------------------------------------------------------------------------------------------

coalesce用法为coalesce(expr1,expr2……exprn),其作用是在expr1,expr2……exprn这列表达式中查找第一个不为null的值且返回该值。如果都为null,则返回null。

官方文档用法解释如下:

COALESCE

Syntax

De.ion of coalesce.gif follows

Purpose

COALESCE returns the first non-null expr in the expression list. You must specify at least two expressions. If all occurrences of expr evaluate to null, then the function returns null.

Oracle Database uses short-circuit evaluation. The database evaluates each expr value and determines whether it is NULL, rather than evaluating all of theexpr values before determining whether any of them is NULL.

If all occurrences of expr are numeric data type or any nonnumeric data type that can be implicitly converted to a numeric data type, then Oracle Database determines the argument with the highest numeric precedence, implicitly converts the remaining arguments to that data type, and returns that data type.

This function is a generalization of the NVL function.

You can also use COALESCE as a variety of the CASE expression. For example,
COALESCE(expr1, expr2)

is equivalent to:
CASE WHEN expr1 IS NOT NULL THEN expr1 ELSE expr2 END

Similarly,
COALESCE(expr1, expr2, ..., exprn)

where n >= 3, is equivalent to:
CASE WHEN expr1 IS NOT NULL THEN expr1
ELSE COALESCE (expr2, ..., exprn) END

Examples

SQL> select list_price,min_price,coalesce(list_price,min_price) from scott.procts;

LIST_PRICE MIN_PRICE COALESCE(LIST_PRICE,MIN_PRICE)

---------- ---------- ------------------------------

10000 8000 10000

20000 20000

30000 30000 30000

到此,nvl、nvl2、nullif、coalesce四个函数用法我们都了解了,看似很简单,但实际结合应用起来,就容易犯混,如下例子:

examples:

Examine the data in the LIST_PRICE and MIN_PRICE columns of the PRODUCTS table:

LIST_PRICE MIN_PRICE

10000 8000

20000

30000 30000

Which two expressions give the same output? (Choose two.)

A. NVL(NULLIF(list_price, min_price), 0)

B. NVL(COALESCE(list_price, min_price), 0)

C. NVL2(COALESCE(list_price, min_price), min_price, 0)

D. COALESCE(NVL2(list_price, list_price, min_price), 0)

Answer: BD

乍看你呢很快给出答案么,下面给出答案解析。

A. NVL(NULLIF(list_price, min_price), 0) 查询结果如下:

SQL> select NVL(NULLIF(list_price, min_price), 0) from scott.procts;

NVL(NULLIF(LIST_PRICE,MIN_PRICE),0)

-----------------------------------

10000

20000

0

B. NVL(COALESCE(list_price, min_price), 0) 查询结果如下:

SQL> select NVL(COALESCE(list_price, min_price), 0) from scott.procts;

NVL(COALESCE(LIST_PRICE,MIN_PRICE),0)

-------------------------------------

10000

20000

30000

C. NVL2(COALESCE(list_price, min_price), min_price, 0)查询结果如下:

SQL> select NVL2(COALESCE(list_price, min_price), min_price, 0) from scott.procts;

NVL2(COALESCE(LIST_PRICE,MIN_PRICE),MIN_PRICE,0)

------------------------------------------------

8000

30000

D. COALESCE(NVL2(list_price, list_price, min_price), 0) 查询结果如下:

SQL> select COALESCE(NVL2(list_price, list_price, min_price), 0) from scott.procts;

COALESCE(NVL2(LIST_PRICE,LIST_PRICE,MIN_PRICE),0)

-------------------------------------------------

10000

20000

30000

由此可见,选BD

9. SQL中 nvl()、coalesce()、decode()这三个函数,如果只是判断非空的话,哪一个效率相比较高

nvl只支持2个参数,这是oracle特有的
coalesce支持不定参数,coalesce(bonus,0,1)应该写成coalesce(bonus,0),最后1个不用写,这是ansi标准函数,在多次外连接时尤其有用,譬如 t1 full join t2 on t1.id=t2.id full join t3 on coalesce(t1.id,t2.id)=t3.id full join t4 on coalesce(t1.id,t2.id,t3.id)=t4.id
decode判断不了非空

10. 求SQL语言中COALESCE字符函数的用法!!!!!

功能:返回其参数中的第一个非空表达式,当你要在n个字段中选取某一个非空值
可以用它,比如下面语句
select Coalesce(null,null,1,2,null)union
select Coalesce(null,11,12,13,null)union
select Coalesce(111,112,113,114,null)

返回结果:
1
11
111