Answer the question
In order to leave comments, you need to log in
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
Answer the question
In order to leave comments, you need to log in
Group not by UsrCDistributionDate, but by MANAGER or better by MANAGER_ID, divide count(*) by the number of days in the period.
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 questionAsk a Question
731 491 924 answers to any question