Просто исключите 201806
в предложении WHERE
как
WITH CTE AS
(
SELECT 201801 Var_Date
UNION
SELECT 201802
UNION
SELECT 201803
UNION
SELECT 201804
UNION
SELECT 201805
UNION
SELECT 201806
UNION
SELECT 201807
UNION
SELECT 201808
UNION
SELECT 201809
UNION
SELECT 201810
UNION
SELECT 201811
UNION
SELECT 201812
UNION
SELECT 201901
UNION
SELECT 201902
)
SELECT COUNT(1) Cnt
FROM CTE
WHERE Var_Date BETWEEN 201801 AND 201812
AND Var_Date != 201806;
Если столбец DATE
, то вы можете сделать как
SELECT COUNT(Var1) VarCount
FROM VarTable V INNER JOIN UserTable U
ON V.UserId = U.UserId --Don't forget the JOIN conditions
WHERE U.Usr_Login = 'bella'
AND
Var_Date BETWEEN '2018-01-01' AND '2018-12-31'
AND
Var_Date NOT BETWEEN '2018-06-01' AND '2018-06-30'
Вы также можете использовать MONTH()
и YEAR()
как
SELECT COUNT(Var1) VarCount
FROM VarTable V INNER JOIN UserTable U
ON V.UserId = U.UserId --Don't forget the JOIN conditions
WHERE U.Usr_Login = 'bella'
AND
MONTH(Var_Date) <> 6
AND
YEAR(Var_Date) = 2018;