Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

SQL: Get average time between 2 dates

Posted on 2013-11-16
3
Medium Priority
?
726 Views
Last Modified: 2013-11-17
I have a SQL Statement that lists CrateDate and PaymentDate for all users:  

SELECT     CONVERT(Datetime, tblUserProfileData.PaymentDate, 103) AS Datetime, aspnet_Membership.CreateDate
FROM         tblUserProfileData INNER JOIN
                      aspnet_Membership ON tblUserProfileData.UserId = aspnet_Membership.UserId
WHERE     (tblUserProfileData.PaymentDate <> '') AND (aspnet_Membership.CreateDate > CONVERT(Datetime, '31.01.2009', 103)) AND 
                      (aspnet_Membership.ApplicationId = '189bdd21-fc7a-123d-8a12-1706b1d6a329')

Open in new window


Now I need to find out how many days there is between CreateDate and PaymentDate for an average user. Example: The SQL returns "10" if the average between CreateDate and PaymentDate is 10 days.

Hope someone please can help me solving this :)
0
Comment
Question by:webressurs
3 Comments
 
LVL 41

Expert Comment

by:Sharath
ID: 39653382
Can you post some sample result of your query and the expected result?
Are you looking for datediff between CreateDate and PaymentDate?
You have used Datetime as column alias. One suggestion is not to use keywords as aliases. If you really want, you need to use it as [Datetime].
0
 
LVL 11

Accepted Solution

by:
John_Vidmar earned 2000 total points
ID: 39653413
SELECT  daysAverage = AVG(DATEDIFF(dd,b.CreateDate,a.PaymentDate))
FROM    tblUserProfileData  a
JOIN    aspnet_Membership   b  ON a.UserId = b.UserId
WHERE   a.PaymentDate <> ''
AND     b.CreateDate > CONVERT(Datetime, '31.01.2009', 103)

Open in new window

0
 
LVL 1

Author Closing Comment

by:webressurs
ID: 39654381
Worked perfect, thank you :)
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

This post looks at MongoDB and MySQL, and covers high-level MongoDB strengths, weaknesses, features, and uses from the perspective of an SQL user.
An alternative to the "For XML" way of pivoting and concatenating result sets into strings, and an easy introduction to "common table expressions" (CTEs). Being someone who is always looking for alternatives to "work your data", I came across this …
Via a live example, show how to shrink a transaction log file down to a reasonable size.
Viewers will learn how the fundamental information of how to create a table.

879 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