如何在复杂查询中将 null 替换为 0

发布于 2024-10-10 14:57:55 字数 449 浏览 3 评论 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

肤浅与狂妄 2024-10-17 14:57:55

您可以使用 ISNULL 函数。

以下是您将如何使用该函数(对于 SQL Server)。

ISNULL(columnName, 0) 

You can use the ISNULL function.

Here is how you will use the function (for SQL Server).

ISNULL(columnName, 0) 
蓝颜夕 2024-10-17 14:57:55

我想更换总计
为空时值为0

这是不可能的情况,因为:

COUNT 函数始终返回一个整数。结果不能为 NULL!

至于将表达式合并为某个默认值(以防 NULL),所有主要数据库中都有执行此操作的函数(例如:COALESCENVLISNULLIFNULL)。典型用途是

FUNCTION_NAME(ExpressionThatMayBeNULL, DefaultWhenNull)

有关具体信息,您应该查阅数据库制造商文档(您可以在线找到它)。

I would like to replace the total
value to 0 when it is null

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 is

FUNCTION_NAME(ExpressionThatMayBeNULL, DefaultWhenNull)

For specifics you should consult you database manufacturers documentation (you can find it online).

殤城〤 2024-10-17 14:57:55

COALESCE 将在 PL/SQL (Oracle) 和 T-SQL (SQL Server) 中执行此操作

,语法为 COALESCE(field1, field2[, fieldN]) - 它将选择左侧第一列具有非空值。

修改您的查询:

SELECT DISTINCT(location),  COALESCE((
    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  
),0)   AS total   
FROM table_fo  pfo
WHERE asofdate = '20110105';

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:

SELECT DISTINCT(location),  COALESCE((
    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  
),0)   AS total   
FROM table_fo  pfo
WHERE asofdate = '20110105';
一指流沙 2024-10-17 14:57:55

正如 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

叫嚣ゝ 2024-10-17 14:57:55

如果您使用 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

梦纸 2024-10-17 14:57:55

您使用什么数据库系统?

  • SQL-Server、MySQL:IFNULL(值,0)
  • Oracle:NVL(值,0)
  • PostgreSQL:COALESCE(值,0)

What DB system are you using?

  • SQL-Server, MySQL: IFNULL(value, 0)
  • Oracle: NVL(value, 0)
  • PostgreSQL: COALESCE(value, 0)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文