简体   繁体   English

从多列中过滤空值

[英]Filtering Nulls from multiple columns

Using SQL Server 2005. 使用SQL Server 2005。

Data is in 2 separate tables and I have only been given write permissions. 数据位于2个单独的表中,仅授予我写入权限。

Data looks like: 数据如下:

DateTime1  |  DateTime2
-----------------------
2012-06-01 | 2012-06-01
2012-06-02 | 2012-06-02
2012-06-04 | 2012-06-05
2012-06-02 | NULL
NULL       | 2012-06-05
2012-06-04 | 2012-06-05
NULL       | NULL

What I am trying to do is be able to count values in which DateTime1 and DateTime2 contain values, DateTime1 contains a date and DateTime2 is NULL, DateTime1 is NULL and DateTime2 contains values. 我想做的是能够计算DateTime1和DateTime2包含值,DateTime1包含日期,DateTime2为NULL,DateTime1为NULL和DateTime2包含值的值。

Overall Im trying to avoid DateTime1 being Null and DateTime2 being null. 总的来说,我试图避免DateTime1为Null和DateTime2为null。

My where statement looks like this: 我的where语句如下所示:

Where (DateTime1 is not null or DateTime2 is not null)

The only problem is it is still showing where both are null values. 唯一的问题是它仍然显示两个都是空值的地方。 Anyone know why this might be happening or how to solve it? 有人知道为什么会发生这种情况或如何解决吗?

Thanks 谢谢

EDIT Full Query as requested by @Lamak 根据@Lamak的要求编辑完整查询

;With [CTE] As (
Select
    TH.ID
    ,AMT
    ,Reason
    ,EffDate
    ,DateReq
    ,CS_ID
    ,ROW_NUMBER()
        Over (Partition By ID Order By [pthPrimeKey] Desc) as [RN]
From
    DateTime1Table as [MC] (nolock)
    Left Join History as [TH] (nolock) on [TH].[ID] = [MC].[ID]
    Left Join Trans as [SUB] (nolock) on [SUB].TransactionReasonCode = [TH].Reason
    Left Join Renew as [RM] (nolock) on [MC].ID = [RM].ID
Where 
    ([MC].[DateTime1] is not null or [RM].[DateTime2] is not null)
    And [PostingDate] = DATEADD(dd, datediff(dd, 1, GetDate()),0)
)
SELECT 
[ID]
,[AMT] as [Earned]
,[Reason] as [Reason]
,[EffDate] as [Eff]
,[DateReq] as [Date_Cancel_Req]
,[pthUserId_Number] as [CSR]
FROM [CTE]
Where RN <= 1

The following will allow rows to be included if 以下将允许包含行,如果

  • only DateTime1 has a value 只有DateTime1有一个值
  • only DateTime2 has a value 只有DateTime2具有值
  • both have values 都有价值

It will exclude rows where both values are NULL. 它将排除两个值均为NULL的行。 Is that what you're after? 那是你追求的吗? (I tried to follow the conversations but got lost, and wish you'd have a simpler repro with sample data - I think the CTE and all the other joins and logic really take away from the actual problem you're having.) (我尝试跟进对话,但迷路了,希望您对样本数据进行简单的复制-我认为CTE和所有其他联接和逻辑确实可以解决您遇到的实际问题。)

WHERE COALESCE([MC].[DateTime1], [RM].[DateTime2]) IS NOT NULL

However, since you're performing a LEFT OUTER JOIN , this may belong in the ON clause for [RM] instead of WHERE . 但是,由于您正在执行LEFT OUTER JOIN ,因此它可能属于[RM]ON子句,而不是WHERE Otherwise you won't know if a row is excluded because the value in a matching row was NULL, or because there was no matching row. 否则,您将不知道是否排除了某行,因为匹配的行中的值为NULL,或者没有匹配的行。 And maybe that's ok, just thought I would mention it. 也许没关系,只是以为我会提到它。

EDIT 编辑

Of course, that clause provides the exact same results as ... 当然,该子句提供与...完全相同的结果。

WHERE ([MC].[DateTime1] is not null or [RM].[DateTime2] is not null)

Want proof? 要证明吗?

DECLARE @a TABLE(id INT, DateTime1 DATETIME);
DECLARE @b TABLE(id INT, DateTime2 DATETIME);

INSERT @a SELECT 1, '20120602' ; INSERT @b SELECT 1, NULL;
INSERT @a SELECT 2, NULL       ; INSERT @b SELECT 2, '20120605';
INSERT @a SELECT 3, '20120604' ; INSERT @b SELECT 3, '20120605';
INSERT @a SELECT 4, NULL       ; INSERT @b SELECT 4, NULL;
INSERT @a SELECT 5, '20120602' ; INSERT @b SELECT 9, NULL;
INSERT @a SELECT 6, NULL       ; INSERT @b SELECT 10, '20120605';
INSERT @a SELECT 7, '20120604' ; INSERT @b SELECT 11, '20120605';
INSERT @a SELECT 8, NULL       ; INSERT @b SELECT 12, NULL;

SELECT * FROM @a AS a LEFT OUTER JOIN @b AS b
ON a.id = b.id
WHERE COALESCE(a.DateTime1, b.DateTime2) IS NOT NULL;

SELECT * FROM @a AS a LEFT OUTER JOIN @b AS b
ON a.id = b.id
WHERE a.DateTime1 IS NOT NULL OR b.DateTime2 IS NOT NULL;

Both queries yield: 这两个查询都产生:

id DateTime1  id   DateTime2
-- ---------- ---- ----------
1  2012-06-02 1    NULL       -- because left is not null
2  NULL       2    2012-06-05 -- because right is not null
3  2012-06-04 3    2012-06-05 -- because neither is null
5  2012-06-02 NULL NULL       -- because of no match
7  2012-06-04 NULL NULL       -- because of no match

So as I suggested in the comment, if you're not seeing the rows you expect, you need to look at other parts of the query. 因此,正如我在评论中建议的那样,如果您没有看到期望的行,则需要查看查询的其他部分。 If you provide sample data and desired results, we can try to help you narrow that down. 如果您提供示例数据和所需结果,我们可以尝试帮助您缩小范围。 As it is, I don't think we know enough about your schema and data to determine where the problem is. 实际上,我认为我们对您的架构和数据了解不足,无法确定问题出在哪里。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM