Solved

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

Posted on 2016-11-07
16
52 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 9
  • 6
16 Comments
 
LVL 49

Expert Comment

by:PortletPaul
ID: 41876942
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 50

Accepted Solution

by:
Vitor Montalvão earned 500 total points
ID: 41876957
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
ID: 41876984
do you have the Transaction per minute process configured and running?

I don't have it.

How I can do that?
0
Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

 
LVL 50

Expert Comment

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

Author Comment

by:michalek19
ID: 41877259
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 50

Expert Comment

by:Vitor Montalvão
ID: 41877273
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
ID: 41877298
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 50

Expert Comment

by:Vitor Montalvão
ID: 41877308
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
 

Author Comment

by:michalek19
ID: 41877353
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 50

Expert Comment

by:Vitor Montalvão
ID: 41877387
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
ID: 41877497
wait, I think I am running this query incorrectly. DO I need to create table firs?
0
 

Author Comment

by:michalek19
ID: 41877503
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
ID: 41877511
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
ID: 41877512
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 50

Expert Comment

by:Vitor Montalvão
ID: 41878237
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
ID: 41878686
Thank you very much
0

Featured Post

Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this article I will describe the Copy Database Wizard method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
How to leverage one TLS certificate to encrypt Microsoft SQL traffic and Remote Desktop Services, versus creating multiple tickets for the same server.
There's a multitude of different network monitoring solutions out there, and you're probably wondering what makes NetCrunch so special. It's completely agentless, but does let you create an agent, if you desire. It offers powerful scalability …
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…

717 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