SQL Query help

Dear expert

Please read the query below, I need first understand this:
R.DatetimeSentToReceiver < DATEADD(MONTH, 1, @ReminderMonth)
What does it do? And then I want to run query every months like Januari then Feb etc. And I dont want it to count everything from the date 2018-01-01 How to do it?

Thanks


DECLARE
            @ReminderMonth date = '2018-01-01',
            @ReminderNumber1 int = 1,
                  @ReminderNumber2 int = 2;
 
SELECT
(
SELECT COUNT(*)
FROM
            Invoice.Reminder R
WHERE
            R.DatetimeSentToReceiver >= @ReminderMonth
            AND R.DatetimeSentToReceiver < DATEADD(MONTH, 1, @ReminderMonth)
            AND R.ReminderNumber = @ReminderNumber1
                  )As 'Reminder 1',
(
SELECT
            COUNT(*)
FROM
            Invoice.Reminder R
WHERE
            R.DatetimeSentToReceiver >= @ReminderMonth
            AND R.DatetimeSentToReceiver < DATEADD(MONTH, 1, @ReminderMonth)
            AND R.ReminderNumber = @ReminderNumber2
                  )As 'Reminder 2'
LVL 1
WeTiAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Vitor MontalvãoMSSQL Senior EngineerCommented:
I need first understand this:
R.DatetimeSentToReceiver < DATEADD(MONTH, 1, @ReminderMonth)
What does it do?
It filters the data to return only rows where the DatetimeSentToReceiver are older than a month or more from the reminder. In your example, before 1st of February of 2018.
0
Vitor MontalvãoMSSQL Senior EngineerCommented:
And I dont want it to count everything from the date 2018-01-01 How to do it?
Create a SQL Agent job and copy your code into a new job step by replacing the declaration of the variable @ReminderMonth as the following:
DECLARE 
    @ReminderMonth date = GETDATE(), 

Open in new window

Now  schedule the job to run at the first day of every months and when it runs it will retrieve the first day of the month (GETDATE() function) and execute the rest of the code.
0
Vitor MontalvãoMSSQL Senior EngineerCommented:
Btw, your code can be improved like this:
DECLARE @ReminderMonth date = GETDATE(), 
 
SELECT
	SUM(CASE ReminderNumber
			WHEN 1 THEN 1 
			ELSE 0
		END) As 'Reminder 1',
	SUM(CASE ReminderNumber
			WHEN 2 THEN 1 
			ELSE 0
		END) As 'Reminder 2'
FROM Invoice.Reminder
WHERE DatetimeSentToReceiver >= @ReminderMonth
	AND DatetimeSentToReceiver < DATEADD(MONTH, 1, @ReminderMonth)

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
WeTiAuthor Commented:
Beautiful solution thank you very much!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
R

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.