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
Solved

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

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

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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
LVL 48

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 48

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 48

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 48

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 48

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Hi all, It is important and often overlooked to understand “Database properties”. Often we see questions about "log files" or "where is the database" and one of the easiest ways to get general information about your database is to use “Database p…
If you have heard of RFC822 date formats, they can be quite a challenge in SQL Server. RFC822 is an Internet standard format for email message headers, including all dates within those headers. The RFC822 protocols are available in detail at:   ht…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

840 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