Q
Problem solve Get help with specific problems with your technologies, process and projects.

# Average date difference

I read your response to this question: Calculating AVG date. I am having problems trying to put this in the correct syntax. I have a begin_date and end_date range and I want to get the average of the (completed_date - assigned_date) so will it be

``` SELECT BETWEEN (begin_date) AND (end_date) AVG (completed_date - assigned_date) ???
```

I am trying to get the average completion time between the two dates.
The earlier column pointed out that AVG only works on numbers, and showed a technique for mapping DATEs to numbers so you could use AVG. However, in this case, we are averaging numbers. (completed_date - assigned_date) is the number of days between assigned_date and completed_date. The technique for averaging numbers is the same whether the numbers quantify days, weeks, dollars or apples: simply use the AVG group function.

If your table is called assignment, you can compute the completion time for each row by saying:

``` SELECT completed_date - assigned_date AS completion_days FROM assignment;
```

This shows the number of days between assigned_date and completed_date, assuming that neither of the dates is NULL. If either date is NULL (or if both are), then the difference will be NULL also.

To get the average, use the regular AVG group function.

``` SELECT AVG (completed_date - assigned_date) AS avg_completion_days FROM assignment;
```

This will compute the average completion time (in days) for all rows where the expression "completed_date - assigned_date" is not NULL, which is probably what you want.

You didn't specify exactly what has to be between begin_date and end_date. The query below requires both completed_date and assigned_date to be in that range. The query also assumes that begin_date and end_date are stored in a one-row table called date_range.

``` SELECT AVG (completed_date - assigned_date) AS avg_completion_days FROM assignment, date_range WHERE assigned_date BETWEEN begin_date AND end_date AND completed_date BETWEEN begin_date AND end_date;
```

Since AVG (x - y) = AVG (x) - AVG (y) (assuming NULLs are handled correctly), you could get the same results by using the technique described in the earlier question:

``` SELECT AVG (completed_date - SYSDATE) - AVG (assigned_date - SYSDATE) AS avg_completion_days FROM assignment, date_range WHERE assigned_date BETWEEN begin_date AND end_date AND completed_date BETWEEN begin_date AND end_date;
```

but since it's longer to type, harder to understand and slower to run, why would you want to?

#### Have a question for an expert?

Get answers from a TechTarget expert on whatever's puzzling you.

You will be able to add details on the next page.

#### Start the conversation

Send me notifications when other members comment.

## SearchDataManagement

• ### PostgreSQL 12 boosts open source database performance

Widely used open source PostgreSQL database platform gets a major update providing users with new SQL query capabilities for JSON...

• ### Data integration vs. ETL in the age of big data

Data integration provides a consistent view of business performance across multiple data sources, though it needs to keep up with...

• ### 5 steps to an improved data quality assurance plan

Consultant David Loshin offers tips on developing a data quality strategy that can help identify data errors before they cause ...

• ### ThoughtSpot BI platform an early adopter of AI

Due to AI, ThoughtSpot's analytics tools have been accessible to citizen data scientists from the start, Andrew Yeung, the ...

• ### ThoughtSpot 6 advances AI power of BI platform

New augmented intelligence and machine learning tools, along with a new mobile app, highlight the release of the new update to ...

• ### Promethium tool taps natural language processing for analytics

The software, called Data Navigation System, was designed to enable non-technical users to make complex SQL requests using plain ...

## SearchSAP

• ### CEO of SAP Bill McDermott abruptly resigns

Bill McDermott, CEO of longtime ERP, CRM and BI software vendor SAP, resigned Thursday triggering a succession plan that promotes...

• ### What is an SAP Intelligent Enterprise? Don't look for a clear answer

What does Intelligent Enterprise actually mean? If the SAP term confuses you, you're not alone. Learn what the experts say and ...

• ### Pros and cons of SAP migration to the cloud

Hosting SAP on cloud services can bring added security and scalability. But organizations must also watch out for issues such as ...

## SearchSQLServer

• ### SQL Server database design best practices and tips for DBAs

Good database design is a must to meet processing needs in SQL Server systems. In a webinar, consultant Koen Verbeeck offered ...

• ### SQL Server in Azure database choices and what they offer users

SQL Server databases can be moved to the Azure cloud in several different ways. Here's what you'll get from each of the options ...

• ### Using a LEFT OUTER JOIN vs. RIGHT OUTER JOIN in SQL

In this book excerpt, you'll learn LEFT OUTER JOIN vs. RIGHT OUTER JOIN techniques and find various examples for creating SQL ...

## TheServerSide.com

• ### Java video games, like Minecraft, keep the language popular

The next generation of Java developers can learn the ins and outs of the language through an unlikely source: video games such as...

• ### Pivotal, Microsoft team up to deliver Azure Spring Cloud

Azure Spring Cloud, jointly developed by Microsoft and Pivotal, lets Spring developers bring apps to the cloud without concern ...

• ### GitHub tempts enterprises with Semmle, security enhancements

With the Semmle semantic code analysis engine freshly added to its quiver, GitHub gives corporate development teams one way to ...

## SearchDataCenter

• ### Essential private cloud migration steps

Executive buy-in, application evaluation and tool configuration are all important parts of cloud selection. With these steps, ...

• ### IBM quantum computers' usefulness in sight -- using binoculars

IBM's Bob Sutor discusses Big Blue's new quantum systems and computation center, the realities of quantum computing today and how...

• ### Addendum sets ASHRAE 90.4 as energy-efficiency standard

The publication of ASHRAE 90.4 in 2016 brought a new set of energy guidelines, but the industry still used Standard 90.1. A new ...

## SearchContentManagement

• ### Digital asset management benefits the future of work

Digital asset management promises to transform digital work, making it easier to tag documents, analyze video and provide image ...

• ### Box cloud content management adds PDF tools, boosts security

Box partners with Adobe in releasing a new set of PDF tools that can be used within Box's content management cloud to enable ...

• ### AI optimizes content management workflows, personalizes CX

AI is poised to revolutionize content management workflows, bringing order to enterprise documents, greater insights into ...

## SearchHRSoftware

• ### Get smarter about HR data security

HR deals with mountains of sensitive employee data and must do a better job of protecting that information. Here's a look at how ...

• ### Walmart, FedEx make case for VR training

The payback from virtual training may be improved retention and shorter training times. An hour-long training period can be ...

• ### Do you know how to improve employee experience? Test yourself

Think you know what it takes to create a positive employee experience for your workers? Take this EX quiz to test your savvy and ...

Close