Quantcast
Channel: Forum Getting started with SQL Server
Viewing all articles
Browse latest Browse all 4729

Looking for help on my company DayEnd process and alerting

$
0
0

Hello and good morning to you all.

In my company I am tasked with writing a SQL query to perform a few actions for our DayEnd processing. First off a little bit of information.

Our DayEnd does not run every day skipping Saturday and Sunday. It always starts during the night after business hours  so our database is ready for the next business day. At the end of the process it forwards the date ahead by one day. Monday night starts at 7 PM, but Tuesday/Wednesday/Thursday/Friday starting at 8 PM. Monday night starts a bit earlier as it is processing for Saturday and Sunday and can take a bit longer. Also Friday night into Saturday is different than the rest as it processes more than any other day and forwards the business day to Monday when DayEnd will be run again.

Here are the things I am looking to perform:

1. Automate the script. DayEnd process starts at 7 PM on Monday; 8 PM Tuesday, Wednesday, Thursday, Friday.

2. Log the progress of DayEnd. Create a database averaging the times that each Task will take.

3. If any DayEnd Task falls out of the expectations of when each task will finish send an email. Sometimes Tasks stall out and the process freezes on a Task without us finding out until hours later.

So those are basic goals right now and a good start. So! Here is the code I was given.

SELECT  CONVERT(DATE,DLOG.BusinessDate) "Business Date"
       ,DPRTYPE.DayEndProcessTypeName
       ,DLOG.StartDtTm
       ,DLOG.FinishDtTm
       ,(DATEDIFF(second,DLOG.StartDtTm,DLOG.FinishDtTm)*1.00)/60 "Minutes"

  FROM DATABASENAME.dbo.DayEndLog DLOG

      INNER JOIN DATABASENAME.dbo.DayEndProcessType DPRTYPE
              ON DLOG.DayEndProcessTypeId = DPRTYPE.DayEndProcessTypeId,
      (SELECT '04/07/2054' "DDATE") AS "QDATE"

WHERE (CONVERT(DATE,DLOG.BusinessDate) BETWEEN QDATE.DDATE AND DATEADD(day,+2,QDATE.DDATE) AND NOT DLOG.DayEndProcessTypeId IN (90,100,110)) OR (CONVERT(DATE,DLOG.BusinessDate) BETWEEN DATEADD(day,+1,QDATE.DDATE) AND DATEADD(day,+3,QDATE.DDATE) AND DLOG.DayEndProcessTypeId IN (90,100,110))

 ORDER BY DLOG.StartDtTm, DLOG.FinishDtTm ASC

And here are the results. Sorry if the formatting is off.

Note: the Business Date is in the future as I'm running this on a test database server. Whenever DayEnd is run it pushes the business day ahead by one so that is why the year is 2054. However the Start and Finish days are correct.

Also I changed the process names to be ambiguous, sorry about that. But the timings are real.

Business Date	DayEndProcessTypeName	                                                                                StartDtTm	        FinishDtTm	        Minutes
2054-04-07	Process 1                                                                                       	2019-10-17 17:03:43.580	2019-10-17 17:05:51.017	2.133333
2054-04-07	Process 2                                                                                        	2019-10-17 17:05:51.017	2019-10-17 17:05:51.017	0.000000
2054-04-07	Process 3                                                                                        	2019-10-17 17:05:51.033	2019-10-17 17:10:32.953	4.683333
2054-04-07	Process 4                                                                                        	2019-10-17 17:10:32.953	2019-10-17 17:10:37.143	0.083333
2054-04-07	Process 5                                                                                        	2019-10-17 17:10:37.143	2019-10-17 17:10:38.220	0.016666
2054-04-07	Process 6                                                                                        	2019-10-17 17:10:38.220	2019-10-17 17:10:48.563	0.166666
2054-04-07	Process 7                                                                                        	2019-10-17 17:10:48.563	2019-10-17 17:10:48.563	0.000000
2054-04-07	Process 8                                                                                           	2019-10-17 17:10:48.563	2019-10-17 17:10:50.643	0.033333
2054-04-07	Process 9                                                                                       	2019-10-17 17:10:50.643	2019-10-17 17:13:14.893	2.400000
2054-04-07	Process 10                                                                                        	2019-10-17 17:13:14.893	2019-10-17 17:13:14.923	0.000000
2054-04-07	Process 11                                                                                       	2019-10-17 17:13:14.923	2019-10-17 17:13:14.970	0.000000
2054-04-07	Process 12                                                                                       	2019-10-17 17:13:14.970	2019-10-17 17:13:14.970	0.000000
2054-04-07	Process 13                                                                                       	2019-10-17 17:13:14.970	2019-10-17 17:14:25.407	1.183333
2054-04-07	Process 14                                                                                       	2019-10-17 17:14:25.407	2019-10-17 17:14:25.407	0.000000
2054-04-07	Process 15                                                                                       	2019-10-17 17:14:25.407	2019-10-17 17:14:25.487	0.000000
2054-04-07	Process 16                                                                                       	2019-10-17 17:14:25.487	2019-10-17 17:14:25.563	0.000000
2054-04-07	Process 17                                                                                       	2019-10-17 17:14:25.563	2019-10-17 17:14:25.563	0.000000
2054-04-07	Process 18                                                                                          	2019-10-17 17:14:25.563	2019-10-17 17:20:53.283	6.466666
2054-04-07	Process 19                                                                                       	2019-10-17 17:20:53.283	2019-10-17 17:20:53.300	0.000000
2054-04-08	Process 20                                                                                       	2019-10-17 17:20:53.300	2019-10-17 17:20:53.317	0.000000
2054-04-08	Process 21                                                                                       	2019-10-17 17:20:53.330	2019-10-17 17:20:55.067	0.033333
2054-04-08      Process 22                                                                                      	2019-10-17 17:20:55.067	2019-10-17 17:20:55.127	0.000000

So that is what I am trying to achieve; automate a way to monitor DayEnd and alert if a problem is detected.


Thanks for your help.



Viewing all articles
Browse latest Browse all 4729

Trending Articles