简体   繁体   中英

Calculate the average difference between two date columns for multiple rows

I want to calculate the average difference between multiple dates: sent_date & view_date.

My table structure look like:

CREATE TABLE `mails` (
  `m_id` int(8) NOT NULL AUTO_INCREMENT,
  `sent_date` date NOT NULL DEFAULT '0000-00-00',
  `view_date` date NOT NULL DEFAULT '0000-00-00',
  PRIMARY KEY (`l_id`)
) ENGINE=InnoDB  DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=3 ;

Example data:

sent_date: 2013-06-01 view_date: 2013-06-02 difference: 2 days

sent_date: 2013-06-01 view_date: 2013-06-05 difference: 4 days

Average: 3 days

Use DATEDIFF() and AVG()

select avg(datediff(view_date, sent_date))
from mails

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM