簡體   English   中英

在同一張表上使用INNER JOIN的多個嵌套SELECT DISTINCT語句

[英]Multiple nested SELECT DISTINCTstatments using INNER JOIN on the same table

我一直試圖編寫一個查詢,以通過發布日期並忽略其余行來僅選擇表中每個'Item No_'中的最新條目。 我一直在遇到各種錯誤。 最后一個令人煩惱,因為我現在看不到錯誤。 這是查詢的代碼段:

SELECT ri.* 
FROM [Rigid Industries$Item Ledger Entry] ri 
INNER JOIN 
  (SELECT DISTINCT [Item No_], MAX([Posting Date]) AS maxDate 
    FROM [Rigid Industries$Item Ledger Entry] 
    WHERE maxDate <= CONVERT(datetime, '2014-01-17 02:33:16.939') 
    GROUP BY maxDate, [Item No_]] 
  ) itDat 
  ON ri.[Item No_] = itDat.[Item No_] 
WHERE ri.[Quantity] > 0 
ORDER BY ri.[Location Code] DESC, ri.[Item No_] ASC, ri.[Posting Date] DESC;

在Peter G和rs的幫助下,我找到了解決方案。

SELECT E.*
FROM [Rigid Industries\$Item Ledger Entry] E
WHERE [Posting Date] = 
    (SELECT MAX(x.[Posting Date])
    FROM [Rigid Industries\$Item Ledger Entry] x 
    WHERE x.[Item No_] = E.[Item No_] 
    AND x.[Posting Date] <= CONVERT(datetime, '$dateYo')
    ) AND
    [Entry No_] = 
    (SELECT MAX(y.[Entry No_])
    FROM [Rigid Industries\$Item Ledger Entry] y
    WHERE y.[Item No_] = E.[Item No_] 
    )
AND E.[Quantity] > 0 
ORDER BY E.[Location Code] DESC, E.[Item No_] ASC, E.[Posting Date] DESC;

您可以將查詢重寫為此

SELECT E.*
    FROM [Rigid Industries$Item Ledger Entry] E
    WHERE [Posting Date] = (SELECT MAX(x.[Posting Date]) FROM 
                            FROM [Rigid Industries$Item Ledger Entry] x  
                            WHERE x.[Item No_] = E.[Item No_] AND 
                x.[Posting Date] <= CONVERT(datetime, '2014-01-17 02:33:16.939'))
AND E.[Quantity] > 0 
ORDER BY E.[Location Code] DESC, E.[Item No_] ASC, E.[Posting Date] DESC;

您有幾個問題。 一種語法錯誤是內部查詢通過maxdate聚合,然后還有其他查詢。 那是沒有必要的。 邏輯問題是您需要在on子句中使用日期:

SELECT ri.* 
FROM [Rigid Industries$Item Ledger Entry] ri INNER JOIN 
     (SELECT [Item No_], MAX([Posting Date]) AS maxDate 
      FROM [Rigid Industries$Item Ledger Entry] 
      WHERE [Posting Date] <= CONVERT(datetime, '2014-01-17 02:33:16.939') 
      GROUP BY [Item No_]] 
     ) itDat 
     ON ri.[Item No_] = itDat.[Item No_] and
        ri.[Posting Date] = itDat.maxDate
WHERE ri.[Quantity] > 0 
ORDER BY ri.[Location Code] DESC, ri.[Item No_] ASC, ri.[Posting Date] DESC;

編寫此類查詢的另一種方法是使用not exists子句以及[Rigid Industries$Item Ledger Entry]([Item No_], [Posting Date])上的索引:

SELECT ri.* 
FROM [Rigid Industries$Item Ledger Entry] ri 
WHERE ri.[Quantity] > 0 AND
      NOT EXISTS (SELECT 1
                  FROM [Rigid Industries$Item Ledger Entry] ri2
                  WHERE ri2.[Item No_] = ri.[Item No_] AND
                        ri2.[Posting Date] > ri.[Posting Date]
                 )
ORDER BY ri.[Location Code] DESC, ri.[Item No_] ASC, ri.[Posting Date] DESC;

這就是說:“讓我從該表中獲得所有行,且名稱太長且太復雜,以使該表中沒有其他行具有相同的項目編號和更長的發布日期。”

[Rigid Industries$Item Ledger Entry]是否具有主鍵或唯一索引? 沒有一個,這可能仍然會給您帶來重復,但是仍然會有所改進:

SELECT ri.* 
FROM [Rigid Industries$Item Ledger Entry] ri 
INNER JOIN 
  (SELECT [Item No_], MAX([Posting Date]) AS maxDate 
    FROM [Rigid Industries$Item Ledger Entry] 
    WHERE [Posting Date] <= CONVERT(datetime, '2014-01-17 02:33:16.939') 
    GROUP BY [Item No_] 
  ) itDat 
  ON ri.[Item No_] = itDat.[Item No_]
 AND ri.[Posting Date] = itDat.maxDate
WHERE ri.[Quantity] > 0 
ORDER BY ri.[Location Code] DESC, ri.[Item No_] ASC, ri.[Posting Date] DESC;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM