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

Find average subscription time (calculate dates)

Posted on 2013-11-14
3
347 Views
Last Modified: 2013-11-15
Hi!

I have a table called "tblUserLog" that contains information about deleted users:

---------------------
tblUserLog
---------------------
UserName (varchar)
CreateDate (datetime)
LogDate (datetime)
SiteId (smallint)

I need to create a Stored Procedure that returns the average time between CreateDate (user registration) and LogDate (user deleted) where  SiteId = '2' and CreateDate > '2013-01-01'.

I need this to find out how many days an average users is registered. Hope someone please can help me solving this :)

Thanks!
0
Comment
Question by:webressurs
3 Comments
 
LVL 32

Assisted Solution

by:awking00
awking00 earned 100 total points
ID: 39648756
select avg(timediff) as avgtime from
(select datediff('dd',createdate, logdate) as timediff
 from yourtable
 where siteid = '2' and createdate > '2013-01-01') as x
0
 
LVL 11

Accepted Solution

by:
jasonduan earned 400 total points
ID: 39648766
Try this:

SELECT AVG(DATEDIFF(DAY, CreateDate, LogDate))
FROM tblUserLog
where  SiteId = '2' and CreateDate > '2013-01-01'
0
 
LVL 1

Author Closing Comment

by:webressurs
ID: 39650837
Thanks!!

Jasonduan code worked perfect!

Awking00 code needed a tiny adjustment:

select avg(timediff) as avgtime from
(select datediff(day,createdate, logdate) as timediff
 from yourtable
 where siteid = '2' and createdate > '2013-01-01') as x
0

Featured Post

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Question has a verified solution.

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

Suggested Solutions

In this article I will describe the Copy Database Wizard method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
How to leverage one TLS certificate to encrypt Microsoft SQL traffic and Remote Desktop Services, versus creating multiple tickets for the same server.
Viewers will learn how the fundamental information of how to create a table.
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

792 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