这是我试图从上到下获得平均间隔的日期列。例如,“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
答案 0 :(得分:1)
nb:添加另一个答案比编辑前者更容易。
您需要使用JOINED"派生表"而不是"相关的子查询"。你会发现这个效率更高。在这里,您需要平均一些值,以便派生表包含一个分组。
要使用先前值转移到下一行的技术,您必须交叉加入一些变量,不要对此进行评论。
order by子句对此技术至关重要。在这里,您必须使用涉及dam_id和birth_date的组合订单,否则您将获得垃圾结果。
希望这些查询能够为您确定逻辑。第一个有助于显示每行的详细逻辑。第二个显示"派生表"在连接之前,第三个显示将派生表连接到源(详细信息)表的效果。
查询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的正确列。
答案 1 :(得分:0)
以下将显示一种方法,使用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 |
第三列显示变量在每行结束时保存的内容,并且该值将被带入下一行以允许计算dat差异。
答案 2 :(得分:0)
我的总体猜测是你需要牛信息的“自我加入”。即该表中的某些行指的是已成为水坝的奶牛,而其他行指的是已成为公牛的公牛。
注意我添加了一些代表水坝的行
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 |