如何对多个列执行where子句

时间:2019-07-03 14:55:03

标签: sql

我试图根据信函类型进行过滤,然后在OL(信函类型)根据输入的日期超过7天后发生OL时进行过滤。

这在sql数据库中。我在联系信件类型和输入日期时遇到一些问题,以仅显示那些距IL(类型)超过7天的消息

SELECT  [ID]
,[EnteredDate]
,[CorrespondenceType]
FROM [PRXXXXXXXX] 
WHERE [EnteredDate] >= '2019-01-01' 
AND ([CorrespondenceType] = 'IL' OR [CorrespondenceType] = 'OL')
AND ([CorrespondenceType] = 'OL'[EnteredDate]) >[CorrespondenceType] = 'IL',[EnteredDate] +7

1 个答案:

答案 0 :(得分:0)

这会给您您所需要的吗?

WITH Get_Data AS
(SELECT  [ID]
,[EnteredDate]
,[CorrespondenceType]
FROM [PRXXXXXXXX] 
WHERE [EnteredDate] >= '2019-01-01' 
And [CorrespondenceType] IN ('IL'. 'OL'))

Select [ID]
, [EnteredDate]
, [CorrespondenceType]
FROM Get_Data
WHERE [CorrespondenceType] = 'IL'
AND [EnteredDate] > (SELECT DATEADD(DAY,7,MAX([EnteredDate])) FROM Get_Data WHERE [CorrespondenceType] = 'OL')