簡體   English   中英

SQL SELECT TOP 1 FOR EACH GROUP

[英]SQL SELECT TOP 1 FOR EACH GROUP

我已經瀏覽了其他問題,但無法完全找到我正在尋找的內容。我有一個 SQL 數據庫,其中有一個名為 InventoryAllocations 的表。 在表中,我有多個 DocumentID 條目,並希望檢索每個唯一 DocumentID 的最后一個條目。 我可以通過做來檢索一個

SELECT  top(1) [UID]
      ,[RecordStatusID]
      ,[CreatedDate]
      ,[CreatedTime]
      ,[CreatedByID]
      ,[OperationType]
      ,[InventoryLocationID]
      ,[DocumentTypeID]
      ,[DocumentID]
      ,[SOJPersonnelID]
      ,[InventorySerialisedItemID]
      ,[TransactionQty]
      ,[TransactionInventoryStatusID]
      ,[Completed]
      ,[CreatedByType]
      ,[RecordTimeStamp]
  FROM [CPData].[dbo].[InventoryAllocations]
  order by DocumentID desc

但我希望它帶回一個包含所有唯一 DocumentID 的列表。希望您能提供幫助。 非常感謝漢娜 x

SELECT TOP 1 WITH TIES 
    [UID]
    ,[RecordStatusID]
    ,[CreatedDate]
    ,[CreatedTime]
    ,[CreatedByID]
    ,[OperationType]
    ,[InventoryLocationID]
    ,[DocumentTypeID]
    ,[DocumentID]
    ,[SOJPersonnelID]
    ,[InventorySerialisedItemID]
    ,[TransactionQty]
    ,[TransactionInventoryStatusID]
    ,[Completed]
    ,[CreatedByType]
    ,[RecordTimeStamp]
FROM 
    [CPData].[dbo].[InventoryAllocations]
ORDER BY
    ROW_NUMBER() OVER(PARTITION BY DocumentID ORDER BY [RecordTimeStamp] DESC);

TOP 1在這里與WITH TIES一起WITH TIES

WITH TIES意味着當ORDER BY = 1SELECT獲取此記錄(因為TOP 1 )和所有其他具有ORDER BY = 1 (因為WITH TIES )。

You can use a RowNumber() Window Function.

SELECT * FROM(
    SELECT 
            ROW_NUMBER() OVER(PARITION BY [DOCUMENTID] ORDER BY [RecordTimeStamp] DESC) AS RowNumber, 
          ,[RecordStatusID]
          ,[CreatedDate]
          ,[CreatedTime]
          ,[CreatedByID]
          ,[OperationType]
          ,[InventoryLocationID]
          ,[DocumentTypeID]
          ,[DocumentID]
          ,[SOJPersonnelID]
          ,[InventorySerialisedItemID]
          ,[TransactionQty]
          ,[TransactionInventoryStatusID]
          ,[Completed]
          ,[CreatedByType]
          ,[RecordTimeStamp]
      FROM [CPData].[dbo].[InventoryAllocations] ) as A 
WHERE RowNumber = 1

這給每條記錄一行,獲取每個文檔 ID,然后給最新的 created_date 一個 row_number 1,在此之前的每一行增加 1。然后我們選擇 rowno 為 1 的記錄以獲取每個文檔的最新創建日期ID:

SELECT [UID]
,[RecordStatusID]
,[CreatedDate]
,[CreatedTime]
,[CreatedByID]
,[OperationType]
,[InventoryLocationID]
,[DocumentTypeID]
,[DocumentID]
,[SOJPersonnelID]
,[InventorySerialisedItemID]
,[TransactionQty]
,[TransactionInventoryStatusID]
,[Completed]
,[CreatedByType]
,[RecordTimeStamp]
FROM
(
SELECT  
[UID]
,[RecordStatusID]
,[CreatedDate]
,[CreatedTime]
,[CreatedByID]
,[OperationType]
,[InventoryLocationID]
,[DocumentTypeID]
,[DocumentID]
,[SOJPersonnelID]
,[InventorySerialisedItemID]
,[TransactionQty]
,[TransactionInventoryStatusID]
,[Completed]
,[CreatedByType]
,[RecordTimeStamp]
,ROW_NUMBER() OVER (PARTITION BY DOCUMENT_ID ORDER BY CreatedDate) DESC AS ROWNO
FROM [CPData].[dbo].[InventoryAllocations] 
)
WHERE ROWNO = 1

基本上是這樣的。

with cte as
(
    SELECT [UID]
        , [RecordStatusID]
        , [CreatedDate]
        , [CreatedTime]
        , [CreatedByID]
        , [OperationType]
        , [InventoryLocationID]
        , [DocumentTypeID]
        , [DocumentID]
        , [SOJPersonnelID]
        , [InventorySerialisedItemID]
        , [TransactionQty]
        , [TransactionInventoryStatusID]
        , [Completed]
        , [CreatedByType]
        , [RecordTimeStamp]
        , ROW_NUMBER() over (partition by DocumentID order by DocumentID desc) as RowNum
    FROM   [CPData].[dbo].[InventoryAllocations]
)

select [UID]
    , [RecordStatusID]
    , [CreatedDate]
    , [CreatedTime]
    , [CreatedByID]
    , [OperationType]
    , [InventoryLocationID]
    , [DocumentTypeID]
    , [DocumentID]
    , [SOJPersonnelID]
    , [InventorySerialisedItemID]
    , [TransactionQty]
    , [TransactionInventoryStatusID]
    , [Completed]
    , [CreatedByType]
    , [RecordTimeStamp]
from cte
where RowNum = 1
order by DocumentID desc

暫無
暫無

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

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