Would like to know why do I keep getting the error message?
Created query with SQL Server Management Studio, my output to display correctly has parameters, and I have tried the following with my query.
What I really would like is to have a specific start and end dates and change selected dates with any other combination of MinDate, MaxDate as follows:
@MinDate DATE = '2020-01-01',
@MaxDate DATE = '2020-02-01';
Totals:
COUNT(Receipt_ID) AS 'Total_Number_Of_Receipts',
SUM(Receipt_Amount) AS 'Total_Amount'
This is the error I get:
Msg 102, Level 15, State 1, Line 5
Incorrect syntax near '.'.Msg 8158, Level 16, State 1, Line 5
'Receipt_Date' has more columns than were specified in the column list.
Query:
DECLARE
@MinDate DATE = '2020-01-01',
@MaxDate DATE = '2020-02-01';
WITH Receipt_Date(day) AS
(
SELECT,
DATEADD(d, -1, GETDATE()) AS 'Yesterday',
COUNT(Receipt_ID) AS 'Total_Number_Of_Receipts',
SUM(Receipt_Amount) AS 'Total_Amount'
FROM
receipts AS r
WHERE
CAST(DATEADD(day, 1, day) AS Date) < @MaxDate
)
SELECT *
FROM receipts AS r
A few things:
1: You've got a comma after the select inside your CTE
2: You need to match the definition of the CTE with the columns being returned, so you can't just have 'day' if you're returning 4 columns in the CTE
3: In your select statement you need to reference the CTE.
This returns, but I've no example data: