找到有效验证之间的标准偏差天数

时间:2013-12-10 04:34:20

标签: mysql sql

我必须计算

  1. 有效验证之间的平均天数
  2. 有效验证之间的天数标准差
  3. 例如我有一个表psverify_interaction

    mysql> show create table psverify_interaction\G
    *************************** 1. row ***************************
           Table: psverify_interaction
    Create Table: CREATE TABLE `psverify_interaction` (
      `interaction_id` int(11) NOT NULL AUTO_INCREMENT,
      `customer_id` int(11) NOT NULL,
      `time_stamp` datetime NOT NULL,
      `request_message_details` text,
      `verification_status` varchar(30) DEFAULT NULL,
      `latitutde` varchar(20) DEFAULT 'NA',
      `longitude` varchar(20) DEFAULT 'NA',
      `ip_address` varchar(45) DEFAULT 'NA',
      `batch_id` int(11) DEFAULT NULL,
      `interaction_channel` varchar(10) DEFAULT NULL,
      PRIMARY KEY (`interaction_id`),
      KEY `customer_id` (`customer_id`)
    ) ENGINE=MyISAM AUTO_INCREMENT=381845 DEFAULT CHARSET=latin1
    1 row in set (0.02 sec)
    

    现在我需要找到每个客户

    1. 有效验证之间的平均天数
    2. 有效验证之间的天数标准差
    3. 对于每个客户的有效验证之间的平均日期可以这样做

      SELECT customer_id, 
      DATEDIFF(MAX(time_stamp),MIN(time_stamp)) / (COUNT(time_stamp)-1) as 'avg days between valid verification'
      FROM psverify_interaction 
      AND verification_status = 'valid' 
      GROUP BY customer_id;
      

      现在我需要找到“每个客户的有效验证之间的天数标准差”,如何在查询中找到它

      示例数据

      SELECT interaction_id,customer_id,time_stamp FROM psverify_interaction WHERE customer_id = 352 and  verification_status = 'valid';
      +----------------+-------------+---------------------+
      | interaction_id | customer_id | time_stamp          |
      +----------------+-------------+---------------------+
      |          60809 |         352 | 2010-04-07 08:22:02 |
      |          88703 |         352 | 2010-05-11 02:08:02 |
      |          88888 |         352 | 2010-05-19 00:20:02 |
      |          88889 |         352 | 2010-05-19 00:28:07 |
      |          90024 |         352 | 2010-08-09 11:09:02 |
      +----------------+-------------+---------------------+
      

      因此,有效验证之间的平均日期可以像

      一样计算
      SELECT customer_id, DATEDIFF(MAX(time_stamp),MIN(time_stamp)) / (COUNT(time_stamp)-1) 
      FROM psverify_interaction 
      WHERE customer_id = 352
      AND verification_status = 'valid' 
      GROUP BY customer_id;
      

0 个答案:

没有答案