0

私は次のクエリを持っています:

SELECT p.IdS, 
       p.PID, 
       a.Destination, 
       d.DelDate, 
       d.DelTimeE, 
       d.DelTimeL,
       td.EDateTime, 
       td.ECode, 
       td.[Source]
FROM Parcel p
LEFT JOIN Party a ON p.IdS = a.IdS
LEFT JOIN DateAndTime d ON p.IdS = d.IdS
LEFT OUTER JOIN 
(SELECT TOP(1) PID, 
               EDateTime, 
               ECode, 
               [Source] 
               FROM TgData 
               WHERE ECode='I' ORDER BY ID ASC) AS td
ON p.PID = td.PID
LEFT JOIN Ships s ON p.IdS = s.ShipId
WHERE s.IsFinalized='1' AND s.DComplete='2012-11-30' AND DelDate IS NOT NULL
ORDER BY Destination ASC

しかし、理由は不明ですが、データベースからの応答では、表示する必要のある値があることを知っていても、td列は常に「NULL」になっている場合があります。クエリは1秒未満で実行されます

私はまた、このようにそれを行うために逆の方法を試しました:

        SELECT p.IdS, 
           p.PID, 
           a.Destination, 
           d.DelDate, 
           d.DelTimeE, 
           d.DelTimeL,
       (SELECT TOP(1) EDateTime FROM TgData WHERE ECode='I' AND PID=p.PID ORDER BY ID ASC) AS EDateTime
       (SELECT TOP(1) ECode FROM TgData WHERE ECode='I' AND PID=p.PID ORDER BY ID ASC) AS ECode
       (SELECT TOP(1) [Source] FROM TgData WHERE ECode='I' AND PID=p.PID ORDER BY ID ASC) AS [Source]
FROM Parcel p
LEFT JOIN Party a ON p.IdS = a.IdS
LEFT JOIN DateAndTime d ON p.IdS = d.IdS
LEFT JOIN Ships s ON p.IdS = s.ShipId
WHERE s.IsFinalized='1' AND s.DComplete='2012-11-30' AND DelDate IS NOT NULL
ORDER BY Destination ASC

これは正しいデータを取得しますが、クエリは470行を表示するのに20秒かかります。:P

ヒントやヒントは素敵です。私はこの24時間、一連の思考にとらわれてきました。

4

1 に答える 1

0

このクエリを試してください

SELECT 
    p.IdS, 
    p.PID, 
    a.Destination, 
    d.DelDate, 
    d.DelTimeE, 
    d.DelTimeL,
    EDT.EDateTime AS EDateTime,
    EC.ECode AS ECode,
    TDL.Source as Source
FROM Parcel p
LEFT JOIN Party a ON p.IdS = a.IdS
LEFT JOIN DateAndTime d ON p.IdS = d.IdS
LEFT JOIN Ships s ON p.IdS = s.ShipId
LEFT JOIN (SELECT PID , TOP(1) EDateTime FROM TgData WHERE ECode='I') as EDT ON EDT.PID = p.PID
LEFT JOIN (SELECT PID , TOP(1) ECode FROM TgData WHERE ECode='I') AS EC ON EC.PID=p.PID
LEFT JOIN (SELECT PID , TOP(1) Source FROM TgData WHERE ECode='I') AS TDL ON TDL.PID=p.PID
WHERE s.IsFinalized='1' 
    AND s.DComplete='2012-11-30' 
    AND DelDate IS NOT NULL
ORDER BY Destination ASC
于 2012-12-04T09:59:34.867 に答える