Solved

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

Posted on 2016-11-07
16
51 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 48

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 49

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
Simplifying Server Workload Migrations

This use case outlines the migration challenges that organizations face and how the Acronis AnyData Engine supports physical-to-physical (P2P), physical-to-virtual (P2V), virtual to physical (V2P), and cross-virtual (V2V) migration scenarios to address these challenges.

 
LVL 49

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 49

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 49

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 49

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 49

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

NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

Question has a verified solution.

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

I have written a PowerShell script to "walk" the security structure of each SQL instance to find:         Each Login (Windows or SQL)             * Its Server Roles             * Every database to which the login is mapped             * The associated "Database User" for this …
In this article I will describe the Detach & Attach 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.
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

726 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