使用 SQL Server 2005。
数据位于 2 个单独的表中,我只获得了写权限。
数据如下:
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
我想要做的是能够计算 DateTime1 和 DateTime2 包含值,DateTime1 包含日期并且 DateTime2 为 NULL,DateTime1 为 NULL 并且 DateTime2 包含值的值。
总体而言,我试图避免 DateTime1 为 Null 并且 DateTime2 为空。
我的where语句如下所示:
Where (DateTime1 is not null or DateTime2 is not null)
唯一的问题是它仍然显示两者都是空值。任何人都知道为什么会发生这种情况或如何解决它?
谢谢
根据@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