SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 Day Average
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

shapper
Constraint Violating Yak Guru

450 Posts

Posted - 10/04/2012 :  13:33:57  Show Profile  Reply with Quote
Hello,

I am counting Users and Files from a database as follows:

select
(select COUNT(*) from dbo.Users) as UsersCount,
(select COUNT(*) from dbo.Files) as FilesCount

I need to get an Average for each one.

Basically:

UsersAverage = UsersCount / Number Days
FilesAverage = FilesCount / Number Days

Both files and users have a DateTime Created Field.

How can I do this?

Thank You,

Miguel

Edited by - shapper on 10/04/2012 13:54:53

sunitabeck
Flowing Fount of Yak Knowledge

5155 Posts

Posted - 10/04/2012 :  14:08:38  Show Profile  Reply with Quote
You can divide by the number of days - for example:
SELECT COUNT(*)/COUNT(DISTINCT createDate) FROM dbo.Users
If the createDate column has time also, you would need to do this to extract only the date portion of it:
SELECT COUNT(*)/COUNT(DISTINCT DATEADD(dd,DATEDIFF(dd,0,createDate),0)) FROM dbo.Users
Go to Top of Page

shapper
Constraint Violating Yak Guru

450 Posts

Posted - 10/04/2012 :  18:51:38  Show Profile  Reply with Quote
quote:
Originally posted by sunitabeck

You can divide by the number of days - for example:
SELECT COUNT(*)/COUNT(DISTINCT createDate) FROM dbo.Users
If the createDate column has time also, you would need to do this to extract only the date portion of it:
SELECT COUNT(*)/COUNT(DISTINCT DATEADD(dd,DATEDIFF(dd,0,createDate),0)) FROM dbo.Users




The problem is that I have dates which do not exist in Users table.
To make it easier I created a Calendar table with a column of type Date which contains all dates.

How can I calculate the average of users considering all dates, using the Calendar table, for a period of @BeginDate to @EndDate.

Thank You,
Miguel
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.08 seconds. Powered By: Snitz Forums 2000