More SQL Query questions

Dear expert

Please help me with a query command you can find below, Now I got problem with variables @P4, @P5, @P6, problem is that I want to show result that minus the value of @P1, @P2, @P3, this  R.DatetimeSentToReceiver < DATEADD(MONTH, 2, @ReminderMonth) adds 2 months value, what i want is @P4 = @P4 - @P1 now I tried add this in query, and it return with error ofc. How to do this in the correct way? There are many way doing this, I need a great solution for it, what Im after is this, data in current months output, data in last month output.  @ReminderMonth will be every months day 1 so that will change too.
 
Any help would be great.

      DECLARE
            @ReminderMonth date = '2018--01-01';

DECLARE @P1 int =  
        (
            SELECT      
        COUNT(*)
        FROM Invoice.Reminder R
        WHERE
        R.DatetimeSentToReceiver >= @ReminderMonth
        AND R.DatetimeSentToReceiver < DATEADD(MONTH, 1, @ReminderMonth)
        AND R.ReminderNumber = 1
            ),
        @P2 int =  
        (
            SELECT      
        COUNT(*)
        FROM Invoice.Reminder R
        WHERE
        R.DatetimeSentToReceiver >= @ReminderMonth
        AND R.DatetimeSentToReceiver < DATEADD(MONTH, 1, @ReminderMonth)
        AND R.ReminderNumber = 2
            ),
        @P3 int =  
        (
            SELECT      
        COUNT(*)
        FROM Invoice.Reminder R
        WHERE
        R.DatetimeSentToReceiver >= @ReminderMonth
        AND R.DatetimeSentToReceiver < DATEADD(MONTH, 1, @ReminderMonth)
        AND R.ReminderNumber = 3
            ),
        @P4 int =  
        (
            SELECT      
        COUNT(*)
        FROM Invoice.Reminder R
        WHERE
        R.DatetimeSentToReceiver >= @ReminderMonth
        AND R.DatetimeSentToReceiver < DATEADD(MONTH, 2, @ReminderMonth)
        AND R.ReminderNumber = 1
        ) ,
        @P5 int =  
        (
            SELECT      
        COUNT(*)
        FROM Invoice.Reminder R
        WHERE
        R.DatetimeSentToReceiver >= @ReminderMonth
        AND R.DatetimeSentToReceiver < DATEADD(MONTH, 2, @ReminderMonth)
        AND R.ReminderNumber = 2
            ) ,
        @P6 int =  
        (
            SELECT      
        COUNT(*)
        FROM Invoice.Reminder R
        WHERE
        R.DatetimeSentToReceiver >= @ReminderMonth
        AND R.DatetimeSentToReceiver < DATEADD(MONTH, 2, @ReminderMonth)
        AND R.ReminderNumber = 3
       ) ;


        SELECT CONVERT(varchar(7),@ReminderMonth,120) AS 'Current month', @P1 AS 'Reminder 1', @P2 AS 'Reminder 2', @P3 AS 'Reminder 3', SUM(@P1+@P2+@P3) AS 'Curry month reminder Total',
LVL 1
WeTiAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
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.

WeTiAuthor Commented:
Now i did resolve this by doing this:
(@P4-@P1) AS 'Last 2 months Reminder 1'
(@P5-@P2) AS 'Last 2 months Reminder 2'
(@P6-@P3) AS 'Last 2 months Reminder 3'

As I said this took time from SQL query to show, too much processes, now I need some better solution.
Mark WillsTopic AdvisorCommented:
Well, you could do the query once and use CASE for the conditions
  SELECT      
        case when R.DatetimeSentToReceiver < DATEADD(MONTH, 1, @ReminderMonth) AND R.ReminderNumber = 1 then count(*) end as P1,
        case when R.DatetimeSentToReceiver < DATEADD(MONTH, 2, @ReminderMonth) AND R.ReminderNumber = 2 then count(*) end as P2,
etc...
        FROM Invoice.Reminder R
        WHERE
        R.DatetimeSentToReceiver >= @ReminderMonth

Open in new window

Could even cascade in a series of case statements or IIF statements.

Let us know if you want to pursue further...
WeTiAuthor Commented:
Thanks, how does the whole query looks like? Sorry man Im abit tired...
Determine the Perfect Price for Your IT Services

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden with our free interactive tool and use it to determine the right price for your IT services. Download your free eBook now!

OMC2000Commented:
The whole query could be like this:

SELECT CONVERT(varchar(7),ReminderMonth,120) "Current month", P1 AS "Reminder 1", P2 AS "Reminder 2", P3 AS "Reminder 3", P1+P2+P3 AS "Curry month reminder Total",
p4 - P1 AS "Reminder 4", p5 - P2 AS "Reminder 5", p6 - P3 AS "Reminder 6"
from
(
select sum(p1) p1,sum(p2) p2,sum(p3) p3,sum(p4) p4,sum(p5) p5,sum(p6) p6, min(ReminderMonth) ReminderMonth
from (              
select
case when DatetimeSentToReceiver< DATEADD(MONTH, 1,ReminderMonth) and ReminderNumber=1 then 1 else 0 end P1,
case when DatetimeSentToReceiver< DATEADD(MONTH, 1,ReminderMonth) and ReminderNumber=2 then 1 else 0 end P2,
case when DatetimeSentToReceiver< DATEADD(MONTH, 1,ReminderMonth) and ReminderNumber=3 then 1 else 0 end P3,
case when DatetimeSentToReceiver< DATEADD(MONTH, 2,ReminderMonth) and ReminderNumber=1 then 1 else 0 end P4,
case when DatetimeSentToReceiver< DATEADD(MONTH, 2,ReminderMonth) and ReminderNumber=2 then 1 else 0 end P5,
case when DatetimeSentToReceiver< DATEADD(MONTH, 2,ReminderMonth) and ReminderNumber=3 then 1 else 0 end P6,
ReminderMonth
from Invoice.Reminder, (select  DATEADD( m, DATEDIFF( m, 0, GETDATE( ) ), 0 )  as ReminderMonth)  x
where DatetimeSentToReceiver>= ReminderMonth)
y
) z

Your variable ReminderMonth is replaced with this query:
(select  DATEADD( m, DATEDIFF( m, 0, GETDATE( ) ), 0 )
which returns the first day of the current month,
Vitor MontalvãoMSSQL Senior EngineerCommented:
Just use the same idea as I showed you yesterday, to reduce the number of SELECTs:
DECLARE @ReminderMonth date = '2018--01-01';

DECLARE @P1, @P2, @P3, @P4, @P5, @P6 int   

SELECT @P1 = SUM
	(CASE 
		WHEN R.ReminderNumber = 1 THEN 1
		ELSE 0
	END),
	@P2 = SUM
	(CASE 
		WHEN R.ReminderNumber = 2 THEN 1
		ELSE 0
	END),
	@P3 = SUM
	(CASE 
		WHEN R.ReminderNumber = 3 THEN 1
		ELSE 0
	END)
FROM Invoice.Reminder R
WHERE R.DatetimeSentToReceiver >= @ReminderMonth
	AND R.DatetimeSentToReceiver < DATEADD(MONTH, 1, @ReminderMonth)

SELECT	@P4 = SUM
	(CASE 
		WHEN R.ReminderNumber = 1 THEN 1
		ELSE 0
	END),
	@P5 = SUM
	(CASE 
		WHEN R.ReminderNumber = 2 THEN 1
		ELSE 0
	END),
	@P6 = SUM
	(CASE 
		WHEN R.ReminderNumber = 3 THEN 1
		ELSE 0
	END),
FROM Invoice.Reminder R
WHERE R.DatetimeSentToReceiver >= @ReminderMonth
	AND R.DatetimeSentToReceiver < DATEADD(MONTH, 2, @ReminderMonth)

Open in new window

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:
Now I realized how much you can do with sql query... thanks all for your kindly help, I will use Vitors solution. Thanks again.
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
Query Syntax

From novice to tech pro — start learning today.