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

Average date

Do you know of an efficient way to calculate the AVG (date) in Oracle9i? I have a complex query that returns predicted dates and I need to average them.

The bad news is: AVG only works on numbers. If you need to average anything else, the best you can do is map the values to numbers, average the numbers, and, if necessary and possible, map back to the original domain.

The good news is: That's quite easy with dates in any version of Oracle. Oracle's date arithmetic does it all for you: you don't even have to use TO_NUMBER or TO_DATE.

``` SELECT SYSDATE + AVG (predict_dt - SYSDATE) FROM table_q;
```

In Oracle, subtracting one date from another results in a number, equal to the number of days between them. (For example, if predict_dt is midnight at the beginning of tomorrow, and it's now 6:00 pm, then predict_dt minus SYSDATE is .25.) When you add a date d to a number n in Oracle, the result is another date, n days later than d. (For example, SYSDATE + .25 is the date .25 days in the future, that is, six hours from now.)

You may want to treat the average as a column in a query, alongside the predicted date and other values. In that case, join in a subquery:

``` SELECT q.predict_dt, s.avg_predict_dt, ... FROM table_q q, ( SELECT SYSDATE + AVG (predict_dt - SYSDATE) AS avg_predict_dt FROM table_q ) s ...;
```

What's so special about SYSDATE that we can use it as the center of our time reckoning system? Nothing at all: SYSDATE is completely arbitrary. You can use any date as point 0.

If you want to, you can use any of these:

• January 1, 4712 BCE
• July 4, 1776
• January 1, 2000

The only thing that matters is that you use the same value when converting from a date to a number (predict_dt - x) and back again (x + AVG ...). I chose SYSDATE because it produces a date with only seven keystrokes.

Now, what if your query is so complex that it takes several minutes to run, and you're concerned about getting an average that's accurate down to the second? Don't worry about SYSDATE changing in the course of a SELECT statement: Oracle will use the time the query started as SYSDATE throughout the entire statement, even if it takes a noticeable amount of time to finish. (Similarly, in long-running INSERT and UPDATE statements, Oracle uses the ending time of the query as SYSDATE for all rows affected.)

Start the conversation

Send me notifications when other members comment.

SearchDataManagement

• Oracle Data Science efforts advance with new services

Oracle adds news new services to its cloud infrastructure platform in a bid to provide data scientists, as well as data analysts,...

• Data governance metrics: Data quality, data literacy and more

Statistics on data quality are the most common metrics used to track data governance programs, but other types of data-related ...

• Snowflake raises \$479 million to boost cloud data warehouse

Snowflake is looking to grow its data warehouse platform with new funding and a partnership with Salesforce that could ...

• MicroStrategy analytics suite fosters insurance co-op's growth

Using MicroStrategy's BI platform, a Canada-based insurance co-operative has been able to spread analytics to end users ...

• Yellowbrick Data, MicroStrategy partnership aims to enhance analytics

The new partnership will enable flexible scalability, support for mixed workloads and multi-cloud support. The goal is to provide...

• Ethical data collection in the spotlight over new regulations

New regulations have put data privacy top of mind for many consumers. Here's a look at how businesses can incorporate ethical ...

SearchSAP

• Take care of data before SAP S/4HANA migration

In this Q&A, Rajesh Rengarethinam of ERP security vendor Appsian discusses why reviewing data security and business processes are...

Business Suite 7 will receive mainstream maintenance until 2027, a decision industry observers say reflects the difficulty of an ...

• SAP cloud revenue up, S/4HANA adoption still slow

Although SAP reported impressive revenue and cloud growth for 2019, it remains challenged to get the majority of its installed ...

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

• Why the 8 Java primitive data types are not objects

What's the difference between Java primitive types and objects? To start, don't classify primitive types as objects. Let's ...

• A brief history of Java: How it forever changed programming

The Java programming language is an important tool in an application developer's quiver. But Java has also undergone many changes...

• How to properly perform Java String comparisons

Don't let incorrect Java String object comparisons bog down your code. Follow this tutorial to understand the differences between...

SearchDataCenter

• Initiative aims to improve data center incident reporting

The Data Center Incident Reporting Network hopes to pull back the smoke screen on software and hardware issues to improve ...

• A guide to GPU implementation and activation

Data center GPU usage goes beyond hardware. Admins must work with vendors and developers to have the right software architecture ...

• Key components for negotiating an HPC colocation contract

When it comes to HPC colocation, there are specialized areas to cover in a service-level agreement. Review provider protocols for...

SearchContentManagement

• Akeneo rolls out newest version of PIM software

Akeneo PIM version 4.0 features digital asset manager, API-integrated connection modules and AI attribute mapping to better ...

• Get to know 4 workflow automation tools

Workflow automation can benefit businesses by making manual processes digital, giving employees more time to work on other tasks....

Organizations that sell content might benefit from a paywall. Here are some questions to ask when deciding if a paywall strategy ...

SearchHRSoftware

• 8 HR technology trends CHROs should investigate in 2020

Many emerging HR technologies are solidly in the realm of 'must-haves,' while others remain hype. Here's some advice to help you ...

• How to attract top talent in a competitive hiring market

Finding the best candidates can seem impossible in today's competitive hiring landscape. Use these methods to help you attract ...

• Federal workforce too reliant on degrees, says White House

In its budget proposal, the Trump administration complains that hiring rules hurt its ability to hire those just out of college ...

Close