My overall guess is that you need a "self join" of the cattle information. i.e. some rows in that table refer to cows that have been dams, and other rows refer to bulls that have been sires.
Note I have ADDED some rows to represent dams
SQL Fiddle
MySQL 5.6 Schema Setup:
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);
Query 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 |