1

私はこれに苦労しているようで、冷たい誰かが私に光を当てることを望んでいました.

petevents には、日付が異なることを除いて、複数の重複したイベントがあります。各ペットイベントの最新のものだけが必要です。

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 に答える