如何选择第一个日期,上一个日期,第一个日期高于参考日期的最新日期

时间:2018-01-19 12:55:57

标签: mysql sql

我想从table1中选择最新日期,第二个最新日期和第一个日期,其中第一个日期高于在另一个table2中找到的参考日期。该参考日期也应该是该表格中的最新日期。我有一个解决方案,应该是。但问题是,如果table1只有1条记录,解决方案将不会返回输出。表格示例:

表1

Reg ID | DateOfAI   | byTechnician
2GP001 | 2015-01-13 | 31
2GP001 | 2015-02-18 | 31
2GP001 | 2017-11-10 | 45
2GP001 | 2017-11-30 | 32
2GP044 | 2017-11-30 | 28
2GP001 | 2017-12-23 | 32

表2

Reg ID | DateOfCalving   | DryOffDate
2GP001 | 2016-01-14      | 
2GP070 | 2016-01-14      |
2GP065 | 2017-04-08      | 
2GP001 | 2017-04-12      | 

我的预期输出是:

Reg ID | LatestDateOfCalving   | 1stDateOfAI  | PreviousAIDate  | LastestAIDate 
2GP001 | 2017-04-12            | 2017-11-10   | 2017-11-30      | 2017-12-23

我从月球和背后到处搜寻......仍然没有运气。这些是我用过的查询

Fisrt:

    SELECT b.actualDam,COUNT(x.actualDam) AS ilanba, max(b.breedDate) AS huli, max(x.breedDate) AS nex,MIN(x.breedDate) AS una,IFNULL(c.calvingDate,NULL) AS nganak,r.*,h.herdID,a.animalID,a.regID, IFNULL(a.dateOfBirth,NULL) AS buho
  FROM x_animal_breeding_rec b
  LEFT JOIN x_animal_calving_rec c ON b.recID=c.brecID 
  LEFT JOIN x_herd_animal_rel r ON b.actualDam=r.animal 
  LEFT JOIN x_herd h ON r.herd=h.herdID 
  LEFT JOIN x_animal_main_info a ON b.actualDam=a.animalID 
  JOIN x_animal_breeding_rec x ON b.actualDam = x.actualDam AND x.breedDate < b.breedDate  
  WHERE h.herdID = ? AND x.mateType = ? AND x.recFlag = ? GROUP BY b.actualDam

和我尝试的第二个是这段代码:

    SELECT b.recID
     , b.actualDam
     , b.breedDate
     , min(b.breedDate) AS una     
     , max(b.breedDate) AS huli
     , COUNT(b.actualDam) AS sundot
     , b.mateType
     , b.recFlag
     , a.animalID
     , a.regID     
     , h.*      
  FROM
     ( SELECT c.recID, c.actualDam           
            , c.breedDate            
            , c.mateType
            , c.recFlag
            , CASE WHEN @prev=c.recID THEN @i:=@i+1 ELSE @i:=1 END i
            , @prev:=c.recID prev
            FROM x_animal_breeding_rec c        
            , ( SELECT @prev:=null,@i:=0 ) vars
            ORDER BY c.recID,c.breedDate DESC
     ) b 
     LEFT JOIN x_animal_main_info a ON b.actualDam=a.animalID 
     LEFT JOIN x_herd_animal_rel h ON b.actualDam=h.animal            
     WHERE i <= 2 GROUP BY b.actualDam HAVING h.herd = ? AND b.mateType = ? AND b.recFlag = ? ORDER BY b.breedDate DESC

此处的另一个问题是第一个解决方案返回错误的计数。第二个解决方案返回CORRECT COUNT,但是返回了错误的日期。我希望你能给我一个主意。提前完成。

1 个答案:

答案 0 :(得分:0)

以下查询回答了您的问题:

SELECT
    RegID,
    LatestDateOfCalving,
    MIN(DateOfAI) AS 1stDateOfAI,
    REPLACE(SUBSTRING_INDEX(GROUP_CONCAT(DateOfAI ORDER BY DateOfAI DESC), ',', 2), CONCAT(MAX(DateOfAI), ','), '') AS PreviousAIDate,
    MAX(DateOfAI) AS LatestAIDate
FROM (
    SELECT
        t1.RegID,
        LatestDateOfCalving,
        DateOfAI,
        IF(DateOfAI >= LatestDateOfCalving, 1, 0) AS dates
    FROM table1 AS t1
    INNER JOIN (
        SELECT
            RegID,
            MAX(DateOfCalving) AS LatestDateOfCalving
        FROM table2 GROUP BY RegID
    ) AS tt2 ON t1.RegID = tt2.RegID) AS x
WHERE dates = 1
GROUP BY RegID
HAVING COUNT(dates) >= 3;

<强> 输出

+--------+---------------------+-------------+----------------+--------------+
| RegID  | LatestDateOfCalving | 1stDateOfAI | PreviousAIDate | LatestAIDate |
+--------+---------------------+-------------+----------------+--------------+
| 2GP001 | 2017-04-12          | 2017-11-10  | 2017-11-30     | 2017-12-23   |
+--------+---------------------+-------------+----------------+--------------+

<强> DEMO

在子查询中,我们从RegID中选择LatestDateOfCalvingtable2以获得参考日期。然后将其加入table1并标记记录DateOfAI 是否大于等于 LatestDateOfCalvingIF(DateOfAI >= LatestDateOfCalving, 1, 0)) 。我们在外部查询(SELECT RegID, LatestDateOfCalving, MIN(DateOfAI) AS 1stDateOfAI, MAX(DateOfAI) AS LatestAIDate, ...)中使用此子查询,并仅选择DateOfAI处于LatestDateOfCalvingWHERE dates = 1之后或之后的那些记录,其中1是其中的标志条件为真)并且至少有3条记录(HAVING COUNT(dates) >= 3)。在外部查询中,我使用REPLACE(SUBSTRING_INDEX(GROUP_CONCAT(...)))结构,以便从逗号(previousAIDate)分隔的日期列表中提取,