Need a sales $ for the past 90 days

Hello Experts,
Need a sales $ for the past 90 days:

Table Name: Sales
Data Available: 2022, 2023 & 2024 YTD

Criteria: Approved Date (Any date from 1st Jan 2024 to till date).
The sales data need for the past 90 days from 20240111.
Ex: If, the Approved Date is 20240111, the 90th day was 2024-10-13 (Approved date - 90 days).
Now, I need to get the "sales" from 2024-10-13 to 2024-01-11.

Note: The current date format is "YYYYMMDD"

Thank You,
Yoga

How to post a T-SQL question on a public forum | spaghettidba

2 Likes

And you tried:

where salesdate between approvedDate and (approvedDate - 90)

Are you wanting to pass in a user generated parameter?


DECLARE @ApprovedDate date;
SET @ApprovedDate = ISNULL(@ApprovedDate, GETDATE());

SELECT *
FROM dbo.Sales
WHERE ApprovedDate >= CONVERT(varchar(8), DATEADD(DAY, -90, @ApprovedDate), 112) AND
    AprovedDate <= CONVERT(varchar(8), @ApprovedDate, 112)