Solved

Find average subscription time (calculate dates)

Posted on 2013-11-14
3
355 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
[X]
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
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

Use Case: Protecting a Hybrid Cloud Infrastructure

Microsoft Azure is rapidly becoming the norm in dynamic IT environments. This document describes the challenges that organizations face when protecting data in a hybrid cloud IT environment and presents a use case to demonstrate how Acronis Backup protects all data.

Question has a verified solution.

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

Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
In the first part of this tutorial we will cover the prerequisites for installing SQL Server vNext on Linux.
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

617 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