我正在使用SQL Server 2017并尝试使用where语句中的“AND / OR”来清理我的数据集
这是我到目前为止所做的,但它无效。
WHERE
([Campaign] NOT LIKE '%Search%')
AND (([Campaign] = '2018 BRAND'
AND [Tactic] <> 'Display'
AND [Date] NOT BETWEEN '2018-01-01' AND '2018-04-27')
OR
([Campaign] = '2017 BRAND'
AND [Date] NOT BETWEEN '2017-01-01' AND '2017-01-07'))
输出:
Row Channel Campaign Tactic Date Visits
----------------------------------------------------------------
1 Digital 2017 Brand Display 1/1/2017 43
2 Search 2018 Search Search 2/3/2018 50
3 Digital 2018 Brand Video 4/10/2018 102
4 Digital 2018 Local Display 4/9/2018 82
5 Digital 2018 eLocal Video 3/28/2018 79
6 Search 2018 Search Search 4/5/2018 30
7 Digital 2017 Local Display 1/4/2017 22
8 Search 2017 Search Search 4/5/2017 21
9 Digital 2018 Brand Display 2/2/2018 43
10 Digital 2018 Brand Display 2/24/2018 71
我想删除的三件事是:
答案 0 :(得分:0)
如果您尝试删除行,那么我认为or
更合适:
WHERE ([Campaign] NOT LIKE '%Search%') OR
NOT ([Campaign] = '2018 BRAND' AND Tactic = 'Display' AND [Date] BETWEEN '2018-01-01' AND '2018-04-27') OR
NOT ([Campaign] = '2017 BRAND' AND [Date] BEWEEN '2017-01-01' and '2017-01-07')
你所拥有的逻辑有点棘手。我的方法是使用NOT
,这样您就可以看到每个条件,并保留一切不匹配的内容。