从 mysql 服务器的日期列中查找平均间隔日期

find average interval dates from the date colum in mysql server

这是我试图从中获取从上到下平均间隔的日期列。例如,第一个间隔在“2008-03-29”和“2009-04-04”之间会有所不同,下一个间隔将是“2009-04-04”和“2010-05-18”之间的天数和列表差异继续。 Click here to see the date list

下面是我写的存储过程部分。

SELECT Cow_ID, Cow_Age, Cow_Breed, count(animal_id) as Numb_calves_born, count(Calf_ID) as numb_calves_weaned, calve_interval, Cow_Sire_Breed  FROM(
    SELECT 
        @cow_id:=cattle_info_tbl.dam_ID as Cow_ID,
        cattle_info_tbl.cow_age as Cow_Age,
        @cow_breed:=(select breed from cattle_info_tbl where animal_id=@cow_id) as Cow_Breed,
        animal_id IN (select animal_id from cattle_info_tbl where dam_id=@cow_id) as animal_id,
        #cattle_info_tbl.animal_id as Calf_ID,
        @cow_sire_id:=(select sire_id from cattle_info_tbl where animal_id=@cow_id) as Cow_Sire_Breed,
        #Where goes the problem
        (MAX(cattle_info_tbl.birth_date)-MIN(cattle_info_tbl.birth_date)/(SUM(CASE WHEN weaning_tbl.manage_code='T' Then 0.5 ELSE 1 END))) as calve_interval


    FROM cattle_info_tbl 
    INNER JOIN measurement_tbl ON (cattle_info_tbl.chaps_id = measurement_tbl.chaps_id) AND entry_type='W'
    INNER JOIN weaning_tbl ON weaning_tbl.chaps_id=cattle_info_tbl.chaps_id
    where cattle_info_tbl.herd_id = input_herd_id AND dam_id!='' AND manage_code=0
    order by dam_id

下面以birth_date的部分样本为例,展示MySQL中比较前一个值和当前值的方法,见SQL Fiddle

设置:

CREATE TABLE Table1
    (`birth_date` datetime)
;

INSERT INTO Table1
    (`birth_date`)
VALUES
    ('2008-03-29 00:00:00'),
    ('2009-04-04 00:00:00'),
    ('2010-05-18 00:00:00'),
    ('2011-03-14 00:00:00'),
    ('2011-05-25 00:00:00')
;

查询:

SELECT
       birth_date
     , IF(@prev_value IS NOT NULL,datediff(birth_date,@prev_value),NULL) difference
     , @prev_value := birth_date
FROM Table1
CROSS JOIN (SELECT @prev_value:=str_to_date(NULL,'%Y-%M-%d')) y
order by birth_date

Results:

|           birth_date | difference | @prev_value := birth_date |
|----------------------|------------|---------------------------|
| 2008-03-29T00:00:00Z |     (null) |       2008-03-29 00:00:00 |
| 2009-04-04T00:00:00Z |        371 |       2009-04-04 00:00:00 |
| 2010-05-18T00:00:00Z |        409 |       2010-05-18 00:00:00 |
| 2011-03-14T00:00:00Z |        300 |       2011-03-14 00:00:00 |
| 2011-05-25T00:00:00Z |         72 |       2011-05-25 00:00:00 |

第三列显示变量在每一行末尾保存的内容,正是该值被带入下一行以允许计算数据差异。

nb:添加另一个答案比编辑前一个答案更容易。

  1. 您需要使用 JOINED "derived tables" 而不是 "correlated subqueries"。你会发现这也更有效率。在这里你需要对一些值进行平均,所以派生的 table 涉及一个分组依据。

  2. 要使用将前一个值传递到下一行的技术,您必须交叉连接一些变量,不要将其注释掉。

  3. order by 子句对该技术至关重要。在这里你必须使用包含 dam_id 和 birth_date 的组合命令,否则你会得到一个垃圾结果。

希望这些查询能为您确定逻辑。第一个帮助显示每一行的详细逻辑。第二个显示的是加入前的"derived table",第三个显示的是将派生的table加入到源中的效果(详细)table.

查询 1:

SELECT
      IF((t2.dam_id=@prev_dam), datediff(t2.birth_date,@prev_value), NULL) difference
    , @prev_dam
    , @prev_value
    , t2.dam_id
    , t2.birth_date
    , @prev_dam := t2.dam_id
    , @prev_value := t2.birth_date
FROM cattle_info_tbl t2
CROSS JOIN (SELECT @prev_dam:=null x, @prev_value:=str_to_date(NULL,'%Y-%M-%d') y) y
order by t2.dam_id, t2.birth_date

Results:

| difference | @prev_dam | @prev_value | dam_id | birth_date | @prev_dam := t2.dam_id | @prev_value := t2.birth_date |
|------------|-----------|-------------|--------|------------|------------------------|------------------------------|
|     (null) |    (null) |      (null) |  S6040 | 2008-04-30 |                  S6040 |                   2008-04-30 |
|        351 |     S6040 |  2008-04-30 |  S6040 | 2009-04-16 |                  S6040 |                   2009-04-16 |
|        336 |     S6040 |  2009-04-16 |  S6040 | 2010-03-18 |                  S6040 |                   2010-03-18 |
|     (null) |     S6040 |  2010-03-18 |  S6093 | 2008-04-04 |                  S6093 |                   2008-04-04 |
|        376 |     S6093 |  2008-04-04 |  S6093 | 2009-04-15 |                  S6093 |                   2009-04-15 |
|        353 |     S6093 |  2009-04-15 |  S6093 | 2010-04-03 |                  S6093 |                   2010-04-03 |
|        344 |     S6093 |  2010-04-03 |  S6093 | 2011-03-13 |                  S6093 |                   2011-03-13 |
|        444 |     S6093 |  2011-03-13 |  S6093 | 2012-05-30 |                  S6093 |                   2012-05-30 |
|        351 |     S6093 |  2012-05-30 |  S6093 | 2013-05-16 |                  S6093 |                   2013-05-16 |
|        362 |     S6093 |  2013-05-16 |  S6093 | 2014-05-13 |                  S6093 |                   2014-05-13 |
|     (null) |     S6093 |  2014-05-13 |  S6094 | 2008-03-29 |                  S6094 |                   2008-03-29 |
|        371 |     S6094 |  2008-03-29 |  S6094 | 2009-04-04 |                  S6094 |                   2009-04-04 |
|        409 |     S6094 |  2009-04-04 |  S6094 | 2010-05-18 |                  S6094 |                   2010-05-18 |
|        300 |     S6094 |  2010-05-18 |  S6094 | 2011-03-14 |                  S6094 |                   2011-03-14 |
|       1185 |     S6094 |  2011-03-14 |  S6094 | 2014-06-11 |                  S6094 |                   2014-06-11 |

查询 2:

SELECT dam_id, AVG(difference) age
FROM (
      SELECT
            IF((t2.dam_id=@prev_dam), datediff(t2.birth_date,@prev_value), NULL) difference
          , t2.dam_id
          , @prev_dam := t2.dam_id
          , @prev_value := t2.birth_date
      FROM cattle_info_tbl t2
      CROSS JOIN (SELECT @prev_dam:=null x, @prev_value:=str_to_date(NULL,'%Y-%M-%d') y) y
      ORDER BY t2.dam_id, t2.birth_date
     ) b
GROUP BY dam_id

Results:

| dam_id |      age |
|--------|----------|
|  S6040 |    343.5 |
|  S6093 | 371.6667 |
|  S6094 |   566.25 |

查询 3:

SELECT
       t1.dam_id as cow_id
     , av.age
FROM cattle_info_tbl t1
LEFT JOIN (
          SELECT dam_id, AVG(difference) age
          FROM (
                SELECT
                      IF((t2.dam_id=@prev_dam), datediff(t2.birth_date,@prev_value), NULL) difference
                    , t2.dam_id
                    , @prev_dam := t2.dam_id
                    , @prev_value := t2.birth_date
                FROM cattle_info_tbl t2
                CROSS JOIN (SELECT @prev_dam:=null x, @prev_value:=str_to_date(NULL,'%Y-%M-%d') y) y
                ORDER BY t2.dam_id, t2.birth_date
               ) b
          GROUP BY dam_id
          ) av ON t1.dam_id = av.dam_id
WHERE t1.herd_id = 'H38' AND t1.dam_id<>''

Results:

| cow_id |      age |
|--------|----------|
|  S6093 | 371.6667 |
|  S6093 | 371.6667 |
|  S6094 |   566.25 |
|  S6094 |   566.25 |
|  S6093 | 371.6667 |
|  S6040 |    343.5 |
|  S6094 |   566.25 |
|  S6093 | 371.6667 |
|  S6040 |    343.5 |
|  S6040 |    343.5 |
|  S6093 | 371.6667 |
|  S6094 |   566.25 |
|  S6093 | 371.6667 |
|  S6094 |   566.25 |
|  S6093 | 371.6667 |

注意:我认为您将 dam_id 与 cow_id 混淆会使一切变得复杂。这似乎不正确。我的猜测是 animal_id 更可能是重新标记为 cow_id.

的正确列

我的总体猜测是你需要一个"self join"的牛人资料。即 table 中的某些行指的是母牛,而其他行指的是公牛。

请注意,我添加了一些行来表示水坝

SQL Fiddle

MySQL 5.6 架构设置:

create table CATTLE_INFO_TBL(
    herd_id     VARCHAR(30),
    chaps_id    BIGINT NOT NULL AUTO_INCREMENT,
    animal_id   varchar(20),
    birth_date  DATE,
    breed       VARCHAR(16),
    reg_no      VARCHAR(30),
    reg_name    VARCHAR(30),
    elec_id     VARCHAR(30),
    sire_chaps_id   BIGINT,
    sire_id     varchar(20),
    dam_chaps_id    BIGINT,
    dam_id      varchar(20),
    cow_age     INT,
    sex     VARCHAR(1), 
    birth_weight    FLOAT,
    birth_weight_status TINYINT(2),
    calving_ease    INT,
    state       VARCHAR(2),
    sex_date    DATE,
    lot_no      varchar(16),
    picture     MEDIUMBLOB,
    PRIMARY KEY (chaps_id)
);

## adding some dams into the detail table
INSERT INTO `cattle_info_tbl` (`herd_id`,`animal_id`,`breed`) VALUES ('H38','S6040','breed of dam');
INSERT INTO `cattle_info_tbl` (`herd_id`,`animal_id`,`breed`) VALUES ('H38','S6093','breed of dam');
INSERT INTO `cattle_info_tbl` (`herd_id`,`animal_id`,`breed`) VALUES ('H38','S6094','breed of dam');

INSERT INTO `cattle_info_tbl` (`herd_id`,`chaps_id`,`animal_id`,`birth_date`,`breed`,`reg_no`,`reg_name`,`elec_id`,`sire_chaps_id`,`sire_id`,`dam_chaps_id`,`dam_id`,`cow_age`,`sex`,`birth_weight`,`birth_weight_status`,`calving_ease`,`state`,`sex_date`,`lot_no`,`picture`) VALUES ('H38',412,'U8104','2008-03-29','ARLOANAN','','','949000000074863',20360,'S6032',10086,'S6094',2,'2',64,NULL,1,'','0000-00-00',NULL,NULL);
INSERT INTO `cattle_info_tbl` (`herd_id`,`chaps_id`,`animal_id`,`birth_date`,`breed`,`reg_no`,`reg_name`,`elec_id`,`sire_chaps_id`,`sire_id`,`dam_chaps_id`,`dam_id`,`cow_age`,`sex`,`birth_weight`,`birth_weight_status`,`calving_ease`,`state`,`sex_date`,`lot_no`,`picture`) VALUES ('H38',467,'W9157','2009-04-04','ARLOANAN','','','982000025313627',20425,'UNKNAR',10086,'S6094',0,'2',0,NULL,0,'','0000-00-00',NULL,NULL);
INSERT INTO `cattle_info_tbl` (`herd_id`,`chaps_id`,`animal_id`,`birth_date`,`breed`,`reg_no`,`reg_name`,`elec_id`,`sire_chaps_id`,`sire_id`,`dam_chaps_id`,`dam_id`,`cow_age`,`sex`,`birth_weight`,`birth_weight_status`,`calving_ease`,`state`,`sex_date`,`lot_no`,`picture`) VALUES ('H38',559,'XMS15','2010-05-18','LOANANGV','','','',-1,'UNKN',10086,'S6094',4,'0',0,NULL,1,'','0000-00-00',NULL,NULL);
INSERT INTO `cattle_info_tbl` (`herd_id`,`chaps_id`,`animal_id`,`birth_date`,`breed`,`reg_no`,`reg_name`,`elec_id`,`sire_chaps_id`,`sire_id`,`dam_chaps_id`,`dam_id`,`cow_age`,`sex`,`birth_weight`,`birth_weight_status`,`calving_ease`,`state`,`sex_date`,`lot_no`,`picture`) VALUES ('H38',593,'Y1057','2011-03-14','LOLOANAN','','','982000172933771',20133,'750S',10086,'S6094',5,'3',80,NULL,1,'','0000-00-00',NULL,NULL);
INSERT INTO `cattle_info_tbl` (`herd_id`,`chaps_id`,`animal_id`,`birth_date`,`breed`,`reg_no`,`reg_name`,`elec_id`,`sire_chaps_id`,`sire_id`,`dam_chaps_id`,`dam_id`,`cow_age`,`sex`,`birth_weight`,`birth_weight_status`,`calving_ease`,`state`,`sex_date`,`lot_no`,`picture`) VALUES ('H38',353,'B4189','2014-06-11','ARLOANAN','','','',20447,'W12',10086,'S6094',8,'3',90,NULL,1,'','0000-00-00',NULL,NULL);
INSERT INTO `cattle_info_tbl` (`herd_id`,`chaps_id`,`animal_id`,`birth_date`,`breed`,`reg_no`,`reg_name`,`elec_id`,`sire_chaps_id`,`sire_id`,`dam_chaps_id`,`dam_id`,`cow_age`,`sex`,`birth_weight`,`birth_weight_status`,`calving_ease`,`state`,`sex_date`,`lot_no`,`picture`) VALUES ('H38',717,'Z2134','2012-05-30','ARLOLOHH','','','949000000096964',20439,'V162',373,'S6093',6,'3',75,NULL,1,'','0000-00-00',NULL,NULL);
INSERT INTO `cattle_info_tbl` (`herd_id`,`chaps_id`,`animal_id`,`birth_date`,`breed`,`reg_no`,`reg_name`,`elec_id`,`sire_chaps_id`,`sire_id`,`dam_chaps_id`,`dam_id`,`cow_age`,`sex`,`birth_weight`,`birth_weight_status`,`calving_ease`,`state`,`sex_date`,`lot_no`,`picture`) VALUES ('H38',221,'A3077','2013-05-16','ARLOLOHH','','','',20447,'W12',373,'S6093',7,'2',75,NULL,1,'','0000-00-00',NULL,NULL);
INSERT INTO `cattle_info_tbl` (`herd_id`,`chaps_id`,`animal_id`,`birth_date`,`breed`,`reg_no`,`reg_name`,`elec_id`,`sire_chaps_id`,`sire_id`,`dam_chaps_id`,`dam_id`,`cow_age`,`sex`,`birth_weight`,`birth_weight_status`,`calving_ease`,`state`,`sex_date`,`lot_no`,`picture`) VALUES ('H38',309,'B4045','2014-05-13','ARLOLOHH','','','',20437,'V131',373,'S6093',8,'3',72,NULL,1,'','0000-00-00',NULL,NULL);
INSERT INTO `cattle_info_tbl` (`herd_id`,`chaps_id`,`animal_id`,`birth_date`,`breed`,`reg_no`,`reg_name`,`elec_id`,`sire_chaps_id`,`sire_id`,`dam_chaps_id`,`dam_id`,`cow_age`,`sex`,`birth_weight`,`birth_weight_status`,`calving_ease`,`state`,`sex_date`,`lot_no`,`picture`) VALUES ('H38',417,'U8163','2008-04-04','ARLOLOHH','','','949000000008829',20360,'S6032',373,'S6093',0,'2',0,NULL,0,'','0000-00-00',NULL,NULL);
INSERT INTO `cattle_info_tbl` (`herd_id`,`chaps_id`,`animal_id`,`birth_date`,`breed`,`reg_no`,`reg_name`,`elec_id`,`sire_chaps_id`,`sire_id`,`dam_chaps_id`,`dam_id`,`cow_age`,`sex`,`birth_weight`,`birth_weight_status`,`calving_ease`,`state`,`sex_date`,`lot_no`,`picture`) VALUES ('H38',478,'W9215','2009-04-15','ARLOLOHH','','','982000128138391',20425,'UNKNAR',373,'S6093',0,'2',0,NULL,0,'','0000-00-00',NULL,NULL);
INSERT INTO `cattle_info_tbl` (`herd_id`,`chaps_id`,`animal_id`,`birth_date`,`breed`,`reg_no`,`reg_name`,`elec_id`,`sire_chaps_id`,`sire_id`,`dam_chaps_id`,`dam_id`,`cow_age`,`sex`,`birth_weight`,`birth_weight_status`,`calving_ease`,`state`,`sex_date`,`lot_no`,`picture`) VALUES ('H38',541,'X0145','2010-04-03','ARLOLOHH','','','999000000013145',20440,'V529',373,'S6093',4,'2',80,NULL,1,'','0000-00-00',NULL,NULL);
INSERT INTO `cattle_info_tbl` (`herd_id`,`chaps_id`,`animal_id`,`birth_date`,`breed`,`reg_no`,`reg_name`,`elec_id`,`sire_chaps_id`,`sire_id`,`dam_chaps_id`,`dam_id`,`cow_age`,`sex`,`birth_weight`,`birth_weight_status`,`calving_ease`,`state`,`sex_date`,`lot_no`,`picture`) VALUES ('H38',589,'Y1052','2011-03-13','ARLOLOHH','','','982000172933719',20425,'UNKNAR',373,'S6093',5,'2',64,NULL,1,'','0000-00-00',NULL,NULL);
INSERT INTO `cattle_info_tbl` (`herd_id`,`chaps_id`,`animal_id`,`birth_date`,`breed`,`reg_no`,`reg_name`,`elec_id`,`sire_chaps_id`,`sire_id`,`dam_chaps_id`,`dam_id`,`cow_age`,`sex`,`birth_weight`,`birth_weight_status`,`calving_ease`,`state`,`sex_date`,`lot_no`,`picture`) VALUES ('H38',436,'U8303','2008-04-30','ARLOANHH','','','949000000077530',20360,'S6032',10082,'S6040',2,'3',70,NULL,1,'','0000-00-00',NULL,NULL);
INSERT INTO `cattle_info_tbl` (`herd_id`,`chaps_id`,`animal_id`,`birth_date`,`breed`,`reg_no`,`reg_name`,`elec_id`,`sire_chaps_id`,`sire_id`,`dam_chaps_id`,`dam_id`,`cow_age`,`sex`,`birth_weight`,`birth_weight_status`,`calving_ease`,`state`,`sex_date`,`lot_no`,`picture`) VALUES ('H38',479,'W9217','2009-04-16','ARLOANHH','','','982000025313610',20360,'S6032',10082,'S6040',0,'2',0,NULL,0,'','0000-00-00',NULL,NULL);
INSERT INTO `cattle_info_tbl` (`herd_id`,`chaps_id`,`animal_id`,`birth_date`,`breed`,`reg_no`,`reg_name`,`elec_id`,`sire_chaps_id`,`sire_id`,`dam_chaps_id`,`dam_id`,`cow_age`,`sex`,`birth_weight`,`birth_weight_status`,`calving_ease`,`state`,`sex_date`,`lot_no`,`picture`) VALUES ('H38',519,'X0061','2010-03-18','ARLOANHH','','','999000000013061',20425,'UNKNAR',10082,'S6040',0,'1',0,NULL,0,'','0000-00-00',NULL,NULL);

查询 1:

SELECT
       dams.animal_id
     , dams.breed
     , dams.cow_age
     , av.avdiff
FROM cattle_info_tbl AS dams
INNER JOIN (
          SELECT dam_id, AVG(difference) avdiff
          FROM (
                SELECT
                      IF((t2.dam_id=@prev_dam), datediff(t2.birth_date,@prev_value), NULL) difference
                    , t2.dam_id
                    , @prev_dam := t2.dam_id
                    , @prev_value := t2.birth_date
                FROM cattle_info_tbl t2
                CROSS JOIN (SELECT @prev_dam:=null x, @prev_value:=str_to_date(NULL,'%Y-%M-%d') y) y
                WHERE t2.herd_id = 'H38' AND t2.dam_id<>''
                ORDER BY t2.dam_id, t2.birth_date
               ) b
          GROUP BY dam_id
          ) av ON dams.animal_id = av.dam_id

Results:

| animal_id |        breed | cow_age |   avdiff |
|-----------|--------------|---------|----------|
|     S6040 | breed of dam |  (null) |    343.5 |
|     S6093 | breed of dam |  (null) | 371.6667 |
|     S6094 | breed of dam |  (null) |   566.25 |