Solved

Find average subscription time (calculate dates)

Posted on 2013-11-14
3
335 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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

I'm trying, I really am. But I've seen so many wrong approaches involving date(time) boundaries I despair about my inability to explain it. I've seen quite a few recently that define a non-leap year as 364 days, or 366 days and the list goes on. …
How to leverage one TLS certificate to encrypt Microsoft SQL traffic and Remote Desktop Services, versus creating multiple tickets for the same server.
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function

948 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now