Solved

SQL: Get average time between 2 dates

Posted on 2013-11-16
3
684 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 40

Expert Comment

by:Sharath
Comment Utility
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 500 total points
Comment Utility
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
Comment Utility
Worked perfect, thank you :)
0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
MS SQL Backup 24 67
Complex SQL 10 31
Numeric sequence in SQL 14 36
SQL Server Error Log - logging period 1 14
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.
The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
Via a live example, show how to setup several different housekeeping processes for a SQL Server.

728 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

9 Experts available now in Live!

Get 1:1 Help Now