查找组的最新行

时间:2014-06-27 12:46:46

标签: sql sql-server-2008-r2

我知道没有“最后一行”,所以我希望我清楚这不是我真正想要的。如果一个特定字段的值按字母顺序排在最后,我想选择表中的行。我会尽力在下面画出来。我有点像新手所以请耐心等待......

表 [订单号],[交货日期],[订单数量],[发货数量],[收单],[收货地址],[发票编号]

我们会多次重新开发发票,发票号码会增加一个字母。这也将更新其他字段值。以下是具有倍数发票的典型集合......

'987654', '2014-05-01 00:00:00', '100',  '90',  'BillToXYZ', 'ShipToXYZ',               '987654A' - NEW RECORD - 
'987654', '2014-05-01 00:00:00', '-100', '-90', 'BillToXYZ', 'ShipToXYZ',               '987654B' - NEW RECORD - 
'987654', '2014-05-01 00:00:00', '100',  '100', 'BillToXYZ', 'ShipToNEWSHIPTOLOCATION', '987654C' - NEW RECORD - 
'987654', '2014-05-01 00:00:00', '10',   '10',  'BillToXYZ', '2ndNEWSHIPTOLOCATION',    '987654D' - NEW RECORD - 

我需要的是查询所有上述字段,并且只返回[发票编号]是最后一个字母(按字母顺序排列)(在本例中为987654D),但也将它与[订购数量]的值和无论[发票编号]如何,所有记录的[发货数量]。

如果我能提供任何其他信息,请告诉我。提前谢谢。

3 个答案:

答案 0 :(得分:1)

可以使用ROW_NUMBER函数获取组中设置ORDER BY降序的最后一行,然后使用过滤器获取值为1的行。 带窗口的SUMMAX有助于获取其他聚合值。

WITH D AS (
  SELECT [Order Number]
       , [Delivery Date]
       , SUM([Order Qty]) OVER (PARTITION BY [Order Number]) [Total Order Qty]
       , [Total Shipped Qty] 
       = SUM([Shipped Qty]) OVER (PARTITION BY [Order Number]) 
       , [Bill To]
       , [Ship To]
       , [Last Invoice Number] 
       = MAX([Invoice Number]) OVER (PARTITION BY [Order Number])
       , ID = ROW_NUMBER() OVER (PARTITION BY [Order Number] 
                                 ORDER BY [Invoice Number] DESC)
  FROM   Table1
)
SELECT [Order Number]
     , [Delivery Date]
     , [Total Order Qty]
     , [Total Shipped Qty]
     , [Bill To]
     , [Ship To]
     , [Last Invoice Number]
FROM   D
WHERE  ID = 1

SQLFiddle demo

答案 1 :(得分:0)

这是你要追求的吗?

编辑2:从OP获得澄清。已编辑我的解决方案以显示完整订单的汇总,但仅显示最新发票的订单项信息。在这里小提琴:http://sqlfiddle.com/#!3/08bb0/6

SELECT t1.[Order Number],
       t2.[Max Invoice Number],
       t1.[Ship To],
       t2.[Sum Order Qty],
       t2.[Sum Shipped Qty]
FROM Table1 t1 INNER JOIN
     (SELECT    [Order Number],
                MAX([Invoice Number] ) AS [Max Invoice Number],
                SUM([Order Qty]) AS [Sum Order Qty],
                SUM([Shipped Qty]) AS [Sum Shipped Qty]   
      FROM     Table1
      GROUP BY [Order Number]) t2
      ON t1.[Invoice Number] = t2.[Max Invoice Number]
ORDER BY t1.[Order Number];

您可以将D发票中的更多列添加到t1的SELECT列表中,就像我使用[发送至]一样。

编辑1:添加分组以便考虑整个表格。公平地说,Martin K.首先发布了这个答案。

答案 2 :(得分:0)

在我看来,您可以按照其他答案的建议使用GROUP BY和聚合,但将其放在子查询中,以便您可以将“主”表链接到它并获取最新发票的详细值。以下代码是否符合您的要求?

CREATE TABLE #Table 
  (
    OrderNumber int, 
    DeliveryDate datetime, 
    OrderQty int, 
    ShippedQty int , 
    BillTo varchar(10), 
    ShipTo varchar(100), 
    InvoiceNumber varchar(20)
  )

INSERT INTO #Table 
  (
    OrderNumber, 
    DeliveryDate, 
    OrderQty, 
    ShippedQty, 
    BillTo, 
    ShipTo, 
    InvoiceNumber
  )

SELECT '987654', '2014-05-01 00:00:00', '100',  '90',  'BillToXYZ', 'ShipToXYZ',               '987654A' UNION
SELECT '987654', '2014-05-01 00:00:00', '-100', '-90', 'BillToXYZ', 'ShipToXYZ',               '987654B' UNION
SELECT '987654', '2014-05-01 00:00:00', '100',  '100', 'BillToXYZ', 'ShipToNEWSHIPTOLOCATION', '987654C' UNION
SELECT '987654', '2014-05-01 00:00:00', '10',   '10',  'BillToXYZ', '2ndNEWSHIPTOLOCATION',    '987654D'


SELECT t.*, s.TotalOrderQty, s.TotalShippedQty
FROM 
    #Table t
     INNER JOIN 
      (
        SELECT 
            OrderNumber, 
            MAX(InvoiceNumber) LastInvoice, 
            SUM(OrderQty) TotalOrderQty, 
            SUM(ShippedQty) TotalShippedQty
        FROM #Table
        GROUP BY OrderNumber
      ) s ON 
        t.OrderNumber = s.OrderNumber AND
        t.InvoiceNumber = s.LastInvoice

DROP TABLE #Table