當前位置:首頁 » 編程語言 » 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