More SQL Query questions

WeTi
WeTi used Ask the Experts™
on
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',
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®

Author

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 Advisor, Page Editor
Distinguished Expert 2018
Commented:
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...

Author

Commented:
Thanks, how does the whole query looks like? Sorry man Im abit tired...
Ensure you’re charging the right price for your IT

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

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,
IT Engineer
Distinguished Expert 2017
Commented:
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

Author

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.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial