Generating output by time interval where there are no values in source - logparser

I have a message log file that contains one line per message sent, with a timestamp rounded to the minute. I can use Logparser to generate a report on the message frequency per minute and output a chart showing the same. Here's the query I'm using to produce the report:
SELECT
QUANTIZE (Date, 60) AS M,
COUNT(*) AS Total
FROM '[LOGFILEPATH]'
GROUP BY M
ORDER BY M
This is great for showing the message traffic rate for those minutes when traffic has been generated.
However, I would like to output a chart that also shows the minute intervals where the message count is 0 - in a bar chart, the bar would be at 0 for that minute. This will show us intervals when there has been no message traffic.
Naturally, this isn't in the source data, because it's not logging "no message". Any ideas how to do this? I actually don't care if it's Logparser - Powershell is something else I'm happy to use. Or even Excel, although I highly prefer a solution that can be scripted without manual intervention.
We're a Windows shop with limited access to dev tools, so no Python, bash, etc solutions, please.
Here's some sample (real, obfuscated) data that contanis a nearly half-hour interval where there is no traffic being logged.
Date,MID,Host,Sender,Recipient,Subject,Last State
2017-10-26 01:56,1078941,smtp3 (10.0.0.156),bounces#example.com,2.O#d.com.au,Notification has been created successfully,Message 1078941 to 2.O#d.com.au received remote SMTP response 'ok'
2017-10-26 01:56,1078938,smtp3 (10.0.0.156),bounces#example.com,a#y.com.au,Notification has been created successfully,Message 1078940 to bounces#example.com received remote SMTP response '2.0.0'
2017-10-26 01:55,4571113,smtp1 (10.0.1.59),bounces#example.com,K#c.com,Notification has been created successfully,Message 4571113 to K#c.com received remote SMTP response '2.0.0'
2017-10-26 01:55,1078936,smtp3 (10.0.0.156),bounces#example.com,G.F#y.com.au,Notification has been created successfully,Message 1078936 to G.F#y.com.au received remote SMTP response 'ok'
2017-10-26 01:54,4571083,smtp1 (10.0.1.59),bounces#example.com,B.H#b.com,Notification has been created successfully,Message 4571083 to B.H#b.com received remote SMTP response 'Mail accepted'.
2017-10-26 01:53,1078927,smtp3 (10.0.0.156),bounces#example.com,S.R#g.com,Notification has been created successfully,Message 1078927 to S.R#g.com received remote SMTP response '2.0.0'
2017-10-26 01:52,4571051,smtp1 (10.0.1.59),bounces#example.com,A.U#r.com.au,Notification has been created successfully,Message 4571051 to A.U#r.com.au received remote SMTP response 'ok'
2017-10-26 01:23,4570598,smtp1 (10.0.1.59),bounces#example.com,T#h.com,Notification has been created successfully,Message 4570598 to T#h.com received remote SMTP response '2.0.0'
2017-10-26 01:23,4570594,smtp1 (10.0.1.59),bounces#example.com,I#k.com.au,Notification has been created successfully,Message 4570594 to I#k.com.au received remote SMTP response 'ok'
2017-10-26 01:22,4570579,smtp1 (10.0.1.59),bounces#example.com,I.C#s.com.au,Notification has been created successfully,Message 4570579 to I.C#s.com.au received remote SMTP response '2.0.0'
2017-10-26 01:22,4570577,smtp1 (10.0.1.59),bounces#example.com,P.M#h.net.au,Notification has been created successfully,Message 4570577 to P.M#h.net.au received remote SMTP response '2.0.0'
2017-10-26 01:22,4570575,smtp1 (10.0.1.59),bounces#example.com,O#h.com.au,Notification has been created successfully,Message 4570575 to O#h.com.au received remote SMTP response '2.0.0'
2017-10-26 01:21,4570571,smtp1 (10.0.1.59),bounces#example.com,O#f.com,Notification has been created successfully,Message 4570571 to O#f.com received remote SMTP response 'ok'
2017-10-26 01:21,4570557,smtp1 (10.0.1.59),bounces#example.com,O.M#m.v.edu.au,Notification has been created successfully,Message 4570557 to O.M#m.v.edu.au received remote SMTP response 'OK'
2017-10-26 01:21,4570549,smtp1 (10.0.1.59),bounces#example.com,A#a.com,Notification has been created successfully,Message 4570550 to bounces#example.com received remote SMTP response '2.0.0'
2017-10-26 01:20,1078803,smtp3 (10.0.0.156),bounces#example.com,M.1#l.com.au,Notification has been created successfully,Message 1078803 to M.1#l.com.au received remote SMTP response '2.0.0'
2017-10-26 01:20,1078802,smtp3 (10.0.0.156),bounces#example.com,B#g.com,New Account,Message 1078802 to B#g.com received remote SMTP response '2.0.0'
2017-10-26 01:20,4570539,smtp1 (10.0.1.59),bounces#example.com,I#v.com.au,Notification has been created successfully,Message 4570539 to I#v.com.au received remote SMTP response 'OK'
2017-10-26 01:19,1078794,smtp3 (10.0.0.156),bounces#example.com,A.T#b.com,Notification has been created successfully,Message 1078794 to A.T#b.com received remote SMTP response 'ok'

As mentioned in the comments it would be a multi step process starting with getting the dates covered in your log file:
SELECT MIN(Date) AS MinDate, MAX(Date) as MaxDate
INTO theDates.csv
FROM yourlog.log
The reading in the dates contained in theDates.csv using say a Powershell program to generate a log file that matched your log format and contained exactly 1 record per minute.
Then you can run your original query with a slight tweak:
SELECT
QUANTIZE(Date, 60) as M,
SUB(COUNT(*), 1) as total
FROM yourlog.log, yourfakelogfile.log
ORDER BY M
GROUP BY M
The slight tweaking being the count is being decremented by 1 so that you get zeros in the time period where there was no activity.
Put all three of these steps in say a powershell script and you have an automated, repeatable process.

Related

How do I compile SQL email but not send?

In SQL Server, it's possible to send an email via Outlook.
I want to compile an email but have it automatically sent. The code below automatically queues the email and sends to the recipients. I need to make adjustments on the email before sending it out. These adjustments account for dates. Is that possible?
EXEC msdb.dbo.sp_send_dbmail
#profile_name = 'Email Team',
#recipients='username#eemail.com',
#subject='Monthly Report',
#body='Good morning, reports have ran for these dates: ',
#body_format='HTML',
#from_address='<username#email.com>'
As a result, I get this:
Mail (Id: 3549) queued.

Email sql query results of multiple email addresses daily

I am fairly green at SQL and have reached a road block. I have a Job that already runs a query and sends an email to our purchasing department agents via the agent.
Here is my sample data they receive as a text attachment:
po_num vend_num qty_needed external_email_addr
318 1 200 email#earthlink.net
318 1 910 email#earthlink.net
703 2 250 email#row.com
993 3 3600 email#cast.com
993 3 3600 email#cast.com
676 4 1 NULL
884 5 10000 email#Futures.com
118 5 2500 email#Futures.com
My goal is to automatically send each vendor one email of the qty_needed using the email address in external_email_addr field. Also, if the email address is NULL it would send me an email that this needs to be fixed.
I am not sure how complicated or simple this is but any help would be greatly appreciated.
Since the po_num is unique you will generate several mails per email address per day based on the example data you provided.
I dont have access to SQL at the moment so the syntax might need some sprucing up.
SELECT po_num,
vend_num,
qty_needed,
CASE WHEN external_email_addr ='' THEN COALESCE(external_email_addr,'defaultempty#fixthisproblem.com') ELSE external_email_ddr END AS email_address
FROM table_name

meaning of the values of sent_status on msdb.dbo.sysmail_mailitems

I am sending emails from SQL Server, and need to map the values of the sent_status column on the msdb.dbo.sysmail_mailitems table to something more descriptive.
So far I have identified two values:
1 = 'Sent'
2 = 'Failed'
Are there any more possible values, and if so what do they represent?
sent_status, --0 new, not sent, 1 sent, 2 failure or 3 retry.
On the MSDN page for the related msdb.dbo.sysmail_allitems table, the description for sent_status says:
The status of the mail. Possible values are:
sent - The mail was sent.
unsent - Database mail is still attempting to send the message.
retrying - Database Mail failed to send the message but is
attempting to send it again.
failed - Database mail was unable to send the message.
Connecting the two views together as follows:
SELECT DISTINCT mi.sent_status, ai.sent_status
FROM
msdb.dbo.sysmail_allitems ai
FULL OUTER JOIN
msdb.dbo.sysmail_mailitems mi ON
ai.mailitem_id = mi.mailitem_id
Will yield a relationship, which can be expressed with the following CASE statement:
SELECT
CASE sent_status
WHEN 0 THEN 'Unsent'
WHEN 1 THEN 'Sent'
WHEN 2 THEN 'Failed'
WHEN 3 THEN 'Retrying'
END AS sent_status_desc
FROM msdb..sysmail_mailitems

when use sp_send_dbmail , got error : mail server failure , The requested name is valid, but no data of the requested type was found)

when i use this code try to send a email from sql server
exec msdb.dbo.sp_send_dbmail
#recipients='yang.liu#hotmail.com',
#body= 'abc',
#subject = 'new request',
#profile_name ='thisProfile'
sql server management show me : Mail queued.
but actually, this email is not been send , when i check log to see the reason :
SELECT TOP 100 [log_id]
,[event_type]
,[log_date]
,[description]
,[process_id]
,[mailitem_id]
,[account_id]
,[last_mod_date]
,[last_mod_user]
FROM [msdb].[dbo].[sysmail_event_log]
order by [log_id] desc
the log show me :
The mail could not be sent to the recipients because of the mail server failure. (Sending Mail using Account 1 (2014-07-10T10:34:17). Exception Message: Could not connect to mail server. (The requested name is valid, but no data of the requested type was found).
could anyone tell me what the reason , thanks

Send an email from SQL using the emails that result from a query

I have a table that contains the order status and email address , If I do a select * statement this is my output
Name. Email Adress Approval code Order states. Qty tickets.
Juan Rivera. Juan #anyemail.com. Null. 1 50
Maria Rosales Maria #anyemail.com. 5567657. 1. 25 Jose Almonte. Jose#anyemail.com. Null. 1. 10
What I would like to do is run a stored procedure everynight and send an email to every user that has a null in the approval code, for example :
Dear : Jose ,
We noticed that your order does not contain an approval code , this order is scheduled to be canceled unless an approval code is entered in the next 72 hours.
Thanks you
Is this possible ?
Please refer SQL SERVER – 2008 – Configure Database Mail – Send Email From SQL Database

Resources