SQL Server COALESCE()函数的创新应用
COALESCE()函数可以接受一系列的值,如果列表中所有项都为空(null),那么只使用一个值。然后,它将返回第一个非空值。这一技巧描述了创造性使用SQL Server 中COALESCE()函数的两种方法。 这里有一个简单的例子:有一个Persons数据表,它有三个字段FirstNam
COALESCE()函数可以接受一系列的值,如果列表中所有项都为空(null),那么只使用一个值。然后,它将返回第一个非空值。这一技巧描述了创造性使用SQL Server 中COALESCE()函数的两种方法。
这里有一个简单的例子:有一个Persons数据表,它有三个字段FirstName、MiddleName和LastName。表中包含以下值:
John A. MacDonald
Franklin D. Roosevelt
Madonna
Cher
Mary Weilage
如果你想用一个字符串列出他们的全名,下面给出了如何使用COALESCE()函数完成此功能:
SELECT FirstName + '' '' +COALESCE(MiddleName,'''')+ '' '' +COALESCE(LastName,'''')
如果你不想每个查询都这样写,列表A显示了如何将它转换成一个函数。这样当你需要使用这个脚本的时候(不管每个列的实际值是什么),可以直接调用该函数并传递三个字段参数。在下面的例子中,我传递给函数的参数是人名,但是你可以用字段名替代得到同样的结果:
SELECT dbo.WholeName(''James'',NULL,''Bond'')
UNION
SELECT dbo.WholeName(''Cher'',NULL,NULL)
UNION
SELECT dbo.WholeName(''John'',''F.'',''Kennedy'')
测试结果如下:
James Bond
Cher
John F. Kennedy
你可能会注意到我们的一个问题,在James Bond这个名字中有两个空格。通过修改@result这一行可以改正这个问题,如下所示:
SELECT @Result = LTRIM(@first + '' '' + COALESCE(@middle,'''') + '' '') + COALESCE(@last,'''')
下面是COALESCE()函数的另一个应用。在本例中,我们将显示一个支付给员工的工资单。问题是对于不同的员工工资标准是不同的(例如,有些员工是按小时支付,按工作量每周发一次工资或是按责任支付)。列表B中是创建一个样表的代码。下面是一些示例记录,每个是一种类型:
1 18.00 40 NULL NULL NULL NULL
2 NULL NULL 4.00 400 NULL NULL
3 NULL NULL NULL NULL 800.00 NULL
4 NULL NULL NULL NULL 500.00 600
用下面的代码在同一列中列出支付给员工的总额(不管它们的支付标准):
SELECT
EmployeeID,
COALESCE(HourlyWage * HoursPerWeek,0)+
COALESCE(AmountPerPiece * PiecesThisWeek,0)+
COALESCE(WeeklySalary + CommissionThisWeek,0)AS Payment
FROM [Coalesce_Demo].[PayDay]
结果如下:
EmployeeID Payment
1 720.00
2 1600.00
3 800.00
4 1100.00
你可能需要在应用程序中多处使用这一计算方法,虽然这种表示可以完成任务,但是看起来不是很美观。下面列出了如何使用一个单独的求和列来完成这项工作:
ALTERTABLE Coalesce_Demo.PayDay
ADD Payment AS
COALESCE(HourlyWage * HoursPerWeek,0)+
COALESCE(AmountPerPiece * PiecesThisWeek,0)+
COALESCE(WeeklySalary + CommissionThisWeek,0)
这样只要使用SELECT *就可以显示预先计算好的结果。
小结
本文介绍了使用COALESCE()函数一些特殊场合和特殊方式。就我的经验看来,COALESCE()函数最常出现在一个具体的内容中,如一个查询或视图或存储过程中。
你可以将COALESCE()放在一个函数中来使用它,也可以通过将它放在一个单独的计算列中优化性能,并总能获得结果

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Go language provides two dynamic function creation technologies: closure and reflection. closures allow access to variables within the closure scope, and reflection can create new functions using the FuncOf function. These technologies are useful in customizing HTTP routers, implementing highly customizable systems, and building pluggable components.

In C++ function naming, it is crucial to consider parameter order to improve readability, reduce errors, and facilitate refactoring. Common parameter order conventions include: action-object, object-action, semantic meaning, and standard library compliance. The optimal order depends on the purpose of the function, parameter types, potential confusion, and language conventions.

The key to writing efficient and maintainable Java functions is: keep it simple. Use meaningful naming. Handle special situations. Use appropriate visibility.

1. The SUM function is used to sum the numbers in a column or a group of cells, for example: =SUM(A1:J10). 2. The AVERAGE function is used to calculate the average of the numbers in a column or a group of cells, for example: =AVERAGE(A1:A10). 3. COUNT function, used to count the number of numbers or text in a column or a group of cells, for example: =COUNT(A1:A10) 4. IF function, used to make logical judgments based on specified conditions and return the corresponding result.

Exception handling in C++ can be enhanced through custom exception classes that provide specific error messages, contextual information, and perform custom actions based on the error type. Define an exception class inherited from std::exception to provide specific error information. Use the throw keyword to throw a custom exception. Use dynamic_cast in a try-catch block to convert the caught exception to a custom exception type. In the actual case, the open_file function throws a FileNotFoundException exception. Catching and handling the exception can provide a more specific error message.

When passing a map to a function in Go, a copy will be created by default, and modifications to the copy will not affect the original map. If you need to modify the original map, you can pass it through a pointer. Empty maps need to be handled with care, because they are technically nil pointers, and passing an empty map to a function that expects a non-empty map will cause an error.

Can't read your favorite book in the Books app? Like other native apps on iOS, the Books app may also face some issues. If the Books app isn't working properly, you should start by force closing the Books app. If this simple method doesn't resolve the application, following the remaining steps should resolve the issue. Fix 1 – Force Close Books App Force close the Books app instance from iPhone. Step 1 – Open the Books app. If it doesn't apply or keeps crashing, ignore it. Step 2 – Next, while the Books app instance is open, swipe from the bottom left corner of your phone. Step 3 – You can view all open applications there. Swipe up to turn off each

There are the following steps for interacting with JSON data through SQL in Golang: Use the json.Unmarshal function to parse JSON data into a Go structure and convert JSON to a structure. Use the database/sql package to access and operate SQL databases and perform operations such as inserts and queries. Combining the above steps, you can build an application based on SQL and JSON in Go to implement functions such as user registration and login.
