?
Solved

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

Posted on 2016-11-07
16
Medium Priority
?
56 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 51

Accepted Solution

by:
Vitor Montalvão earned 2000 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
Get real performance insights from real users

Key features:
- Total Pages Views and Load times
- Top Pages Viewed and Load Times
- Real Time Site Page Build Performance
- Users’ Browser and Platform Performance
- Geographic User Breakdown
- And more

 
LVL 51

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 51

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 51

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 51

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 51

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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
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.
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …

801 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