Solved

I am looking for query to generate # transaction per hour and per day

Posted on 2016-11-07
16
35 Views
Last Modified: 2016-11-08
Hi

Would you please help me to create query to generate # transaction per hour and per day in SQL.
Here is a link that creates # transaction per minute
https://www.experts-exchange.com/questions/28976962/Defind-RTO-and-PTO-for-one-of-db-instance.html

Thx, M
0
Comment
Question by:michalek19
  • 9
  • 6
16 Comments
 
LVL 48

Expert Comment

by:PortletPaul
Comment Utility
The essence of what you need is to build a table which has a row for each hour between a start ate/time and an end date/time. There are several way to do this (e.g. using a recursive common table expression, or a table valued function, or just a "tally table"). Once  you have that set of rows-per-hour use a left join to your transactions and then aggregate to wanted count.

It would be helpful to know much more about your specific requirements, e.g.
Are you looking to just run this as a dynamic query, or to store the results pregressively?

Can you provide some "sample data" and the "expected result" please?
0
 
LVL 45

Accepted Solution

by:
Vitor Montalvão earned 500 total points
Comment Utility
michalek, do you have the Transaction per minute process configured and running?
If so this is only a matter of performing the right query:
;WITH BeginData AS
(
	SELECT capturedon BeginDate, database_name, cntr_value
	FROM TransPerMin
	WHERE capturedon=(SELECT MIN(capturedon) FROM TransPerMin)
),
EndData AS
(
	SELECT capturedon EndDate, database_name, cntr_value
	FROM TransPerMin
	WHERE capturedon=(SELECT MAX(capturedon) FROM TransPerMin)
)
SELECT B.database_name, E.cntr_value-B.cntr_value TransElapsed, DATEDIFF(second,B.BeginDate,E.EndDate) SecondsElapsed, (E.cntr_value*1.0-B.cntr_value*1.0)/DATEDIFF(second,B.BeginDate,E.EndDate)*60*60 TransHour
FROM BeginData B 
	INNER JOIN EndData E ON B.database_name=E.database_name

Open in new window

Basically you just need to change the last column in the last SELECT. Above is the example for hour but for a day (last 24h) should be: (E.cntr_value*1.0-B.cntr_value*1.0)/DATEDIFF(second,B.BeginDate,E.EndDate)*60*60*24 TransDay
0
 

Author Comment

by:michalek19
Comment Utility
do you have the Transaction per minute process configured and running?

I don't have it.

How I can do that?
0
 
LVL 45

Expert Comment

by:Vitor Montalvão
Comment Utility
How I can do that?
Well, it was what I posted in your previous question.
0
 

Author Comment

by:michalek19
Comment Utility
Hi

I ran your query and I got results per hr.
So I am assuming when I will run this query again in about an hour the  #transaction  per hour will different. Is this correct?

M
Transperhr.xlsx
0
 
LVL 45

Expert Comment

by:Vitor Montalvão
Comment Utility
So I am assuming when I will run this query again in about an hour the  #transaction  per hour will different. Is this correct?
If you configured a job to collect and stored the data as I posted in the another question, then yes, you're correct.
0
 

Author Comment

by:michalek19
Comment Utility
Here are # trans per day.
It looks like I am getting results.

If I will run this transaction today late afternoon, am I going to have the same results or different?
When I should run this query every day in the morning or during peak hours
numbertranperday.xlsx
0
 
LVL 45

Expert Comment

by:Vitor Montalvão
Comment Utility
If I will run this transaction today late afternoon, am I going to have the same results or different?
It should be different depending on the activity load of your database.

When I should run this query every day in the morning or during peak hours
When you want. It's a report only.
0
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 

Author Comment

by:michalek19
Comment Utility
database_name      TransElapsed         SecondsElapsed          TransHour

ABSCore.System             13466            274                           4246213.139

How do you read this number: 4246213.139?

Is it 4,246,213 per day?
 Is this even possible to have that kind of amount of transaction per day?
0
 
LVL 45

Expert Comment

by:Vitor Montalvão
Comment Utility
Is this even possible to have that kind of amount of transaction per day?
It is but not for your case. You can see that only 274 seconds has been elapsed so you can't use the TransHour yet. One hour has 3600 seconds.
0
 

Author Comment

by:michalek19
Comment Utility
wait, I think I am running this query incorrectly. DO I need to create table firs?
0
 

Author Comment

by:michalek19
Comment Utility
Do I need to create table first

DROP TABLE TransPerMin
GO
CREATE TABLE TransPerMin
(
      capturedon datetime,
      database_name nchar(128),
      cntr_value bigint
)
GO
0
 

Author Comment

by:michalek19
Comment Utility
Than I  need to run :

INSERT INTO TransPerMin (capturedon, database_name, cntr_value)
SELECT GETDATE(), instance_name, cntr_value
FROM sys.dm_os_performance_counters
WHERE counter_name = 'transactions/sec'
0
 

Author Comment

by:michalek19
Comment Utility
The last execute this query

;WITH BeginData AS
(
      SELECT capturedon BeginDate, database_name, cntr_value
      FROM TransPerMin
      WHERE capturedon=(SELECT MIN(capturedon) FROM TransPerMin)
),
EndData AS
(
      SELECT capturedon EndDate, database_name, cntr_value
      FROM TransPerMin
      WHERE capturedon=(SELECT MAX(capturedon) FROM TransPerMin)
)
SELECT B.database_name, E.cntr_value-B.cntr_value TransElapsed, DATEDIFF(second,B.BeginDate,E.EndDate) SecondsElapsed, (E.cntr_value*1.0-B.cntr_value*1.0)/DATEDIFF(second,B.BeginDate,E.EndDate)*60*60 TransHour
FROM BeginData B
      INNER JOIN EndData E ON B.database_name=E.database_name
0
 
LVL 45

Expert Comment

by:Vitor Montalvão
Comment Utility
Yes, create the table first.
The 2nd query (INSERT INTO ...) should be placed in a SQL Agent job and scheduled to run every minute or so.
Then the 3rd query is for reporting only, i.e. you run it when you pretend to obtain statistical information about the number of transaction per second/minute/hour or day.
0
 

Author Closing Comment

by:michalek19
Comment Utility
Thank you very much
0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now