Once you've declared the @Quarter variable or parameter, you'll run into another error:
Invalid column name 'Quarter'.
You can't reference an alias introduced in the SELECT clause within the WHERE clause of the same statement. You either have to use a sub-query, a common table expression, or duplicate the logic of the alias in the WHERE clause.
SELECT Quarter
FROM
(
SELECT CASE
WHEN InvoiceMonth IN (1,2,3) THEN 1
WHEN InvoiceMonth IN (4,5,6) THEN 2
WHEN InvoiceMonth IN (7,8,9) THEN 3
WHEN InvoiceMonth IN (10,11,12) THEN 4
END As Quarter
FROM viewSalesTracker_Model1
)
WHERE Quarter = @Quarter;
Or:
WITH cteQuarters As
(
SELECT CASE
WHEN InvoiceMonth IN (1,2,3) THEN 1
WHEN InvoiceMonth IN (4,5,6) THEN 2
WHEN InvoiceMonth IN (7,8,9) THEN 3
WHEN InvoiceMonth IN (10,11,12) THEN 4
END As Quarter
FROM viewSalesTracker_Model1
)
SELECT Quarter
FROM cteQuarters
WHERE Quarter = @Quarter;
Or:
SELECT CASE
WHEN InvoiceMonth IN (1,2,3) THEN 1
WHEN InvoiceMonth IN (4,5,6) THEN 2
WHEN InvoiceMonth IN (7,8,9) THEN 3
WHEN InvoiceMonth IN (10,11,12) THEN 4
END As Quarter
FROM viewSalesTracker_Model1
WHERE CASE
WHEN InvoiceMonth IN (1,2,3) THEN 1
WHEN InvoiceMonth IN (4,5,6) THEN 2
WHEN InvoiceMonth IN (7,8,9) THEN 3
WHEN InvoiceMonth IN (10,11,12) THEN 4
END = @Quarter;
"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer