R
R
Romario212018-06-22 10:04:16
SQL
Romario21, 2018-06-22 10:04:16

MSSQL query (search for the average value per day for a period)?

MSSQL
Hello everyone, comrades, I ask for help with the request.
There is a range -> last 90 days. The task is to calculate the average value of the number of entries made by the user per day.
Here is my query (so far it displays the total number of records per user for the last 90 days):

SELECT count(*) AS ALLDATA,(SELECT Name FROM Contact WHERE Id='CB3548C1-0339-4386-BE7D-FD08FF7F6DB7') AS MANAGER FROM ORDERS WHERE OwnerId='CB3548C1-0339-4386-BE7D-FD08FF7F6DB7' AND (UsrCDistributionDate BETWEEN GETDATE()-90 AND GETDATE()) GROUP BY UsrCDistributionDate

The task is to get one line: average number, manager
5b2ca6a03642d669807095.png

Answer the question

In order to leave comments, you need to log in

2 answer(s)
R
res2001, 2018-06-22
@res2001

Group not by UsrCDistributionDate, but by MANAGER or better by MANAGER_ID, divide count(*) by the number of days in the period.

N
nozzy, 2018-06-22
@nozzy

something like this:

select
t2.`name` as manager,
AVG(t1.OwnerId) as average_value
from orders t1
inner join contact t2 on t2.id = t1.OwnerId
where t1.OwnerId='CB3548C1-0339-4386-BE7D-FD08FF7F6DB7'
and (UsrCDistributionDate BETWEEN GETDATE()-90 AND GETDATE())
group by t2.`name`

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question