如何在复杂查询中将 null 替换为 0
我想当总值为空时将其替换为 0
以下是查询:
SELECT DISTINCT(location), (
SELECT Count(a.location) as total
FROM table_fo a
LEFT JOIN table_info b ON a.TRADEID = b.TRADEID AND a.asofdate = b.asofdate
WHERE (b.TERMSTATUS <> 'TRAN' OR b.TERMSTATUS is NULL) AND b.asofdate = '20110105' AND a.location = pfo.location
GROUP BY a.LOCATION
) AS total
FROM table_fo pfo
WHERE asofdate = '20110105';
I would like to replace the total value to 0 when it is null
Here is the query:
SELECT DISTINCT(location), (
SELECT Count(a.location) as total
FROM table_fo a
LEFT JOIN table_info b ON a.TRADEID = b.TRADEID AND a.asofdate = b.asofdate
WHERE (b.TERMSTATUS <> 'TRAN' OR b.TERMSTATUS is NULL) AND b.asofdate = '20110105' AND a.location = pfo.location
GROUP BY a.LOCATION
) AS total
FROM table_fo pfo
WHERE asofdate = '20110105';
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
您可以使用 ISNULL 函数。
以下是您将如何使用该函数(对于 SQL Server)。
You can use the ISNULL function.
Here is how you will use the function (for SQL Server).
这是不可能的情况,因为:
COUNT 函数始终返回一个整数。结果不能为 NULL!
至于将表达式合并为某个默认值(以防 NULL),所有主要数据库中都有执行此操作的函数(例如:
COALESCE
、NVL
、ISNULL
、IFNULL
)。典型用途是有关具体信息,您应该查阅数据库制造商文档(您可以在线找到它)。
This is an impossible situation because:
The COUNT function always returns an integer. The result cannot be NULL!
As for coalescing an expression to a certain default in case it is NULL there are functions that do this in all major databases (ex.:
COALESCE
,NVL
,ISNULL
,IFNULL
). The typical use isFor specifics you should consult you database manufacturers documentation (you can find it online).
COALESCE
将在 PL/SQL (Oracle) 和 T-SQL (SQL Server) 中执行此操作,语法为
COALESCE(field1, field2[, fieldN])
- 它将选择左侧第一列具有非空值。修改您的查询:
COALESCE
will do this in PL/SQL (Oracle) and T-SQL (SQL Server)Syntax is
COALESCE(field1, field2[, fieldN])
- it will select the first column from the left to have a non-null value.Modifying the query you had:
正如 dparker 所说,ISNULL(...) 适用于某些类型的 sql,尽管该函数的名称可能因数据库提供程序而异。
例如,IBM DB2 中的函数称为 COALESCE(...),而在 Oracle SQL 中则称为 NVL(...)。
这可能有用
http://www.w3schools.com/sql/sql_isnull.asp
As dparker said, ISNULL(...) will work for some types of sql, though the name of the function can vary among database providers.
The function in IBM DB2 is called COALESCE(...), and in Oracle SQL it is NVL(...) for example.
This may be usefull
http://www.w3schools.com/sql/sql_isnull.asp
如果您使用 SQL Server,则可以使用以下 3 种方法将 null 替换为任何用户定义的替代值。
1.ISNULL
2. 合并
3. CASE
这个问题在我参加的一次采访中也被问到。这是带有示例的文章的链接。顺便说一句,本文中还有一个相同的视频。
替换 NULL 的不同方法在 SQL Server 中
If you are using SQL server, the following are the 3 ways that can be used to replace a null with any user defined substitute value.
1. ISNULL
2. COALESCE
3. CASE
This question is also asked in one of the interviews I attended. Here is the link for an article with examples. By the way, there's also a video on the same in this article.
Different ways to replace NULLS in SQL Server
您使用什么数据库系统?
IFNULL(值,0)
NVL(值,0)
COALESCE(值,0)
What DB system are you using?
IFNULL(value, 0)
NVL(value, 0)
COALESCE(value, 0)