I want to find out how many times a SQL Server database was hit on a given date range. Is there any query which I can use to get hourly transactions (db hits - Select/Insert/update/Delete) for a given date range?
E.g I want to find hourly transaction count on my db on the first of the month from 8 am to 6 pm
The output is expected to be something like
Range
TXN Count
8-9
xxxxxx
9-10
yyyyyy
.
.
17-18
ZZZZZ
You can use the master tables to have the number of connections and sessions
Then, you just need to do a group by the column connect_time or login_time / user
Here you have all master dm exec types
https://learn.microsoft.com/en-us/sql/relational-databases/system-dynamic-management-views/sys-dm-exec-query-stats-transact-sql?view=sql-server-ver15
SELECT * from master.sys.dm_exec_sessions
SELECT * from master.sys.dm_exec_connections
---Sample
SELECT
count(*) as numberConnections,
CAST(login_Time as date) as mydate,
login_name
from
master.sys.dm_exec_sessions
Group by
CAST(login_Time as date),
login_name
Related
I have a sqlite3 database maintained on an AWS exchange that is regularly updated by a Python script. One of the things it tracks is when any team generates a new post for a given topic. The entries look something like this:
id
client
team
date
industry
city
895
acme industries
blueteam
2022-06-30
construction
springfield
I'm trying to create a table that shows me how many entries for construction occur each day. Right now, the entries with data populate, but they exclude dates with no entries. For example, if I search for just
SELECT date, count(id) as num_records
from mytable
WHERE industry = "construction"
group by date
order by date asc
I'll get results that looks like this:
date
num_records
2022-04-01
3
2022-04-04
1
How can I make sqlite output like this:
date
num_records
2022-04-02
3
2022-04-02
0
2022-04-03
0
2022-04-04
1
I'm trying to generate some graphs from this data and need to be able to include all dates for the target timeframe.
EDIT/UPDATE:
The table does not already include every date; it only includes dates relevant to an entry. If no team posts work on a day, the date column will jump from day 1 (e.g. 2022-04-01) to day 3 (2022-04-03).
Given that your "mytable" table contains all dates you need as an assumption, you can first select all of your dates, then apply a LEFT JOIN to your own query, and map all resulting NULL values for the "num_records" field to "0" using the COALESCE function.
WITH cte AS (
SELECT date,
COUNT(id) AS num_records
FROM mytable
WHERE industry = "construction"
GROUP BY date
ORDER BY date
)
SELECT dates.date,
COALESCE(cte.num_records, 0) AS num_records
FROM (SELECT date FROM mytable) dates
LEFT JOIN cte
ON dates.date = cte.date
I have table like below:
I have to calculate daily average count of session for each user.
First i calculated, total no of sessions for each day for every user and from that i tried to calculate average of daily session. I understand that it wont work since all users dont have sessions for every date.Some dates are missing for all users. Is there any way to calculate daily average when there is no entries for some dates
WITH daily_count AS
(
SELECT user_id, to_char(local_time,’MM/DD/YYYY’) AS Date, count(session_id) AS total_count
FROM table_name
GROUP BY device_id, to_char(local_time,’MM/DD/YYYY’)
)
SELECT user_id , AVG(total_count) AS average_session_count
FROM daily_count
GROUP BY user_id
For eg: The max date in the above given table is Feb04 and the min date is Jan31 .So the total no of days is 5 days.If we take Userid 1, it is having records only for 2 dates. So the query i wrote will calculate average for 2 days not for 5 days. How to make it to calculate average for 5 days
if for date 1,2,3 number of sessions for one user is 1,0(no sessions),5 then what output do you want in average sessions? --> 2 or 3?
You need to change your main query as follows:
SELECT USER_ID,
AVG(TOTAL_COUNT) AS AVERAGE_SESSION_COUNT -- if you want output as 3 then use this
--SUM(TOTAL_COUNT)/(MAX(DATE) - MIN(DATE) + 1) -- if you want output as 2 then use this
FROM DAILY_COUNT
GROUP BY USER_ID
I want to create a view or table that counts eg. total number of students on that day the query is executed and add row results each day. Problem is the date column on table changes everyday to the current date.
SELECT
COUNT(*) AS no_of_Students
,CAST(GETDATE() AS DATE) as DATE
FROM mySchool
WHERE students=1
No of student . Date
-----------------------
8 . 2019.02.06
15 . 2019.02.07
(next row should auto update for next day when running the query)
You should not be using GETDATE(), You need to pick the date column you have in your mySchool table.
You need to write your query like following.
SELECT
COUNT(*) as [no_of_Students]
,CAST([DateCoulumn] AS DATE) as [DATE]
FROM [mySchool]
GROUP BY CAST(DateCoulumn AS DATE)
ORDER BY CAST(DateCoulumn AS DATE)
Note: You need to replace DateCoulumn with the correct column name.
I'm querying imported data that has a date/time field that I can't format as date in the table.
Sample:
Ticket Name Date
INC000101 User1 9/5/2016 10:00:34AM
INC000102 User2 9/5/2016 12:02:00PM
INC000103 User1 9/7/2016 3:34:00PM
INC000104 User2 10/1/2016 9:30:23AM
INC000105 User1 10/5/2016 10:20:00AM
INC000106 USer2 10/6/2016 4:56:00PM
I'm trying to get a count of how many tickets each user has per month. Because the Date field comes from the database as a text field, I can't seem to make that format as date/time so I use "left" to filter by month. This is what I've used to get a return on a single User item for the month of October.
SELECT COUNT(*)
FROM 2016YTD
WHERE [Name]='User1' AND left(Date,3) = '10/';
I would like to add counts for User2 through UserX per month so that I can get a count row or column for each the quantity of tickets for each user each month in one report. Everything I've tried won't save the query due to syntax errors in one form or another. I've tried variations of the following query help post as well without success.
SELECT a.distributor_id,
(SELECT COUNT(*) FROM myTable WHERE level='personal' and distributor_id = a.distributor_id) as PersonalCount,
(SELECT COUNT(*) FROM myTable WHERE level='exec' and distributor_id = a.distributor_id) as ExecCount,
(SELECT COUNT(*) FROM myTable WHERE distributor_id = a.distributor_id) as TotalCount
FROM myTable a ;
I'm sure the answer is staring at me, just not sure what at the moment.
Thanks for reading
John
This is only an answer to your first question about how to deal with dates that are stored in text fields.
To get the count for all users for every month you can do:
SELECT [Name], Format([Date],'mmmm') AS Month, COUNT(*) as Count
FROM 2016YTD
GROUP BY [Name], Format([Date],'mmmm')
A text field containing a date that is always in the same format can be treated as a date with Format() so Format([Date],'mmmm') returns the full month name for each date.
you should just need conditional aggregation. I haven't looked at access in a while but probably just something like this:
SELECT
a.distributor_id
,Format([Date],'mmmm') as Month
,SUM(IIF(level='personal',1,0)) as PersonalCount
,SUM(IIF(level='exec',1,0)) as ExecCount
,COUNT(*) as TotalCount
FROM
myTable a
GROUP BY
a.distributor_id
,Format([Date],'mmmm')
I need help with an SQL query that will get me a count of hourly deposits to an account for a specific date. The database consists of deposits and time of deposit to various accounts. I need a query that will return the number deposits made to a certain account on an hourly basis. Ideally the result would look something like this:
MM/DD/YYYY Account XYZ :
12:00PM : 3
1:00PM : 5
2:00PM: 7
3:00PM; 11
*Formatting doesnt really matter, just as long as I can get this info.
Try:
DECLARE #CheckDate DATE
SET #CheckDate='2014/5/13'
SELECT DATEPART(hh, DespositDate), COUNT(1)
FROM table t
WHERE account = 'XYZ' AND CAST(DepositDate AS DATE)=#CheckDate
GROUP BY DATEPART(hh, DespositDate)
You can use the date/time functions for aggregation. In your case, this would look like:
select cast(DepositDate as date), datepart(hour, DespositDate), count(*)
from table t
where account = 'XYZ'
group by cast(DepositDate as date), datepart(hour, DespositDate);