1

我似乎正在为此苦苦挣扎,我希望有人冷冷地为我提供一些启示。

petevents 将有多个重复的事件,但日期会不同。我只想要每个 petevent 中最新的一个。

SELECT name,
       pets.id petid,
       petroutineschedules.id,
       petroutineschedules.eventid,
       timeunit,
       dateunit,
       petevents.startdate
FROM   pets
       LEFT OUTER JOIN petroutineschedules
         ON pets.id = petroutineschedules.petid
       LEFT OUTER JOIN petevents
         ON pets.id = petevents.petid
            AND petevents.eventid = petroutineschedules.eventid
WHERE  ( pets.id = 1025 )
       AND ( pets.deletedat IS NULL )
GROUP  BY petevents.startdate,
          name,
          pets.id,
          petroutineschedules.id,
          petroutineschedules.eventid,
          timeunit,
          dateunit  

来自@aleroot 的更新查询

SELECT name, pets.id petid,  petroutineschedules.id prsid, petroutineschedules.eventid,    timeunit,  dateunit,  petevents.startdate,  petevents.id peid
FROM
pets

LEFT OUTER JOIN
    petroutineschedules ON pets.id = petroutineschedules.petid

LEFT OUTER JOIN
(
    SELECT * 
    FROM petevents p1 
    WHERE p1.startdate = 
        (
            SELECT MAX(startdate) FROM petevents p2 
            WHERE p1.petid = p2.petid
        ) 
) AS petevents
    ON pets.id = petevents.petid 
        AND petevents.eventid = petroutineschedules.eventid    

WHERE (pets.id = 1025) AND (pets.deletedat IS NULL)
GROUP BY petevents.startdate , name , pets.id , petroutineschedules.id ,petroutineschedules.eventid , timeunit , dateunit
ORDER BY eventid
4

3 回答 3

2

如果您可以有重复的事件,您必须获得最新的事件,请尝试以下方式:

SELECT 
name,
pets.id petid,
petroutineschedules.id,
petroutineschedules.eventid,
timeunit,
dateunit,
petevents.startdate
FROM
pets
    LEFT OUTER JOIN
petroutineschedules ON pets.id = petroutineschedules.petid
    LEFT OUTER JOIN
(SELECT * FROM petevents p1 WHERE p1.startdate = 
(SELECT MAX(startdate) FROM petevents p2 WHERE p1.petid = p2.petid) ) ON pets.id = petevents.petid and petevents.eventid = petroutineschedules.eventid
WHERE
(pets.id = 1025) AND (pets.deletedat IS NULL)
GROUP BY petevents.startdate , name , pets.id , petroutineschedules.id , petroutineschedules.eventid , timeunit , dateunit

基本上修改后的部分是这个:

LEFT OUTER JOIN
    (SELECT * FROM petevents p1 WHERE p1.startdate = 
    (SELECT MAX(startdate) FROM petevents p2 WHERE p1..petid = p2.petid) ) ON pets.id = petevents.petid and petevents.eventid = petroutineschedules.eventid

我加入了一个获取最新事件的子查询......

于 2012-05-20T16:30:46.733 回答
1

创建一个视图并左连接其他表。

CREATE VIEW  PETEVNT_VW AS SELECT PETID,EVENTID, MAX(STARTDATE) AS MAXSTARTDATE GROUP BY PETID,EVENTID
于 2012-05-20T16:30:26.227 回答
0
Select 
name,
pets.id petid,
petroutineschedules.id,
petroutineschedules.eventid,
timeunit,
dateunit,
petevents.startdate
from (
SELECT 
name,
pets.id petid,
petroutineschedules.id,
petroutineschedules.eventid,
timeunit,
dateunit,
petevents.startdate,
Rank() over (Partition BY petevents.startdate order by petevents.startdate DESC) as Rank
FROM
pets
    LEFT OUTER JOIN
petroutineschedules ON pets.id = petroutineschedules.petid
    LEFT OUTER JOIN
petevents ON pets.id = petevents.petid and petevents.eventid = petroutineschedules.eventid
WHERE
(pets.id = 1025) AND (pets.deletedat IS NULL)
GROUP BY petevents.startdate , name , pets.id , petroutineschedules.id , petroutineschedules.eventid , timeunit , dateunit
)
where rank = 1
于 2012-05-20T16:44:44.387 回答