最早日期的订单位置,不返回每个日期戳的记录

时间:2019-01-14 20:16:51

标签: sql-server tsql

我有这个子查询,需要返回每个机架的SUM(qty),然后按最早的最早日期(ASC)对机架/箱进行排序。每当我使用日期字段对数据进行排序时,它都会为每个日期返回一条记录。我在SELECT语句中添加了日期字段以供参考。我将如何去做?

SELECT InventoryItems_1.ItemID,
       SUM(ISNULL(InventoryItems_1.QtyToStock, 0)) AS InvQty,
       InventoryItems_1.Rack,
       InventoryItems_1.Bin,
       InventoryItems_1.LocationID,
       MIN(InventoryItems_1.Date),
       Locations_1.LocationCode,
       Locations_1.DescriptionMed
FROM dbo.InventoryItems AS InventoryItems_1
     INNER JOIN dbo.Locations AS Locations_1 ON InventoryItems_1.LocationID = Locations_1.LocationID
WHERE(InventoryItems_1.OwnerDetailID IS NULL)
GROUP BY InventoryItems_1.ItemID,
         InventoryItems_1.LocationID,
         InventoryItems_1.Rack,
         InventoryItems_1.Bin,
         InventoryItems_1.Date,
         Locations_1.LocationCode,
         Locations_1.DescriptionMed
HAVING InventoryItems_1.ItemID = 10308
ORDER BY InventoryItems_1.LocationID,
         InventoryItems_1.Date,
         InventoryItems_1.Rack,
         InventoryItems_1.Bin;

这是我的结果:

ID      Qty     Rack    Bin     Loc     Date                LocID
10308   35      21      02-Z    7   2018-10-22 14:48:33.000     WI  
10308   52.5    21      02-Z    7   2018-10-23 08:18:44.000     WI  
10308   87.5    18      01-Z    7   2018-10-23 12:19:09.000     WI  
10308   87.5    23      01-B    7   2018-10-24 11:02:35.000     WI  
10308   35      19      09-Z    7   2018-12-06 14:24:14.000     WI  
10308   22.5    19      09-Z    7   2018-12-06 16:52:26.000     WI  
10308   30      19      09-Z    7   2018-12-07 07:55:59.000     WI  
10308   55      19      09-Z    7   2018-12-07 08:54:55.000     WI  
10308   32.5    19      09-Z    7   2018-12-07 09:47:19.000     WI  
10308   87.5    19      03-C    7   2018-12-07 11:36:20.000     WI  
10308   72.5    19      10-Z    7   2018-12-07 13:17:03.000     WI  
10308   15      19      10-Z    7   2018-12-07 14:30:38.000     WI  
10308   32.5    18      07-A    7   2018-12-17 13:39:39.000     WI  
10308   12.5    19      03-A    7   2018-12-17 14:48:57.000     WI  
10308   42.5    19      03-A    7   2018-12-18 08:07:42.000     WI  
10308   87.5    19      11-Z    7   2018-12-18 10:11:23.000     WI  
10308   87.5    19      06-B    7   2018-12-18 12:08:17.000     WI  
10308   87.5    18      03-Z    7   2018-12-26 13:40:34.000     WI  
10308   55      21      05-Z    7   2018-12-26 14:48:58.000     WI  
10308   32.5    21      05-Z    7   2018-12-27 07:49:27.000     WI  
10308   87.5    19      01-B    7   2018-12-27 09:55:59.000     WI  
10308   8       18      07-A    7   2018-12-28 09:40:11.000     WI  
10308   0.5     18      08-B    7   2018-12-28 09:40:11.000     WI  
10308   75.5                    9   2018-11-27 11:55:17.000     NJ  
10308   7                       10  2018-10-24 08:28:26.000     TX  
10308   2.5                     10  2018-11-02 10:07:27.000     TX  
10308   12.5                    10  2018-11-02 14:36:57.000     TX  
10308   10.5                    10  2018-11-27 13:56:11.000     TX

这就是我想要的样子,首先按最早的库存日期排序。

ItemID  InvQty  Rack    Bin     Loc LocCode 
10308   87.5    18      01-Z    7   WI  
10308   87.5    18      03-Z    7   WI  
10308   40.5    18      07-A    7   WI  
10308   0.5     18      08-B    7   WI  
10308   87.5    19      01-B    7   WI  
10308   55      19      03-A    7   WI  
10308   87.5    19      03-C    7   WI  
10308   87.5    19      06-B    7   WI  
10308   175     19      09-Z    7   WI  
10308   87.5    19      10-Z    7   WI  
10308   87.5    19      11-Z    7   WI  
10308   87.5    21      02-Z    7   WI  
10308   87.5    21      05-Z    7   WI  
10308   87.5    23      01-B    7   WI  
10308   75.5                    9   NJ  
10308   32.5                    10  TX  

2 个答案:

答案 0 :(得分:2)

您正在按InventoryItems_1.Date字段进行分组,这就是为什么每个日期都有单独的行出现的原因。以此将查询更改为“不分组”,并命名最小日期字段将使您生成正确的输出。另外,如果要将报告限制为仅一个ID,请将其放在WHERE子句中,而不要放在HAVING子句中。这将在执行GROUP BY计算等之前过滤记录,从而加快输出速度。

SELECT InventoryItems_1.ItemID,
       SUM(ISNULL(InventoryItems_1.QtyToStock, 0)) AS InvQty,
       InventoryItems_1.Rack,
       InventoryItems_1.Bin,
       InventoryItems_1.LocationID,
       MIN(InventoryItems_1.Date) As MinDate,
       Locations_1.LocationCode,
       Locations_1.DescriptionMed
FROM dbo.InventoryItems AS InventoryItems_1
INNER JOIN dbo.Locations AS Locations_1 
    ON InventoryItems_1.LocationID = Locations_1.LocationID
WHERE InventoryItems_1.OwnerDetailID IS NULL
    AND InventoryItems_1.ItemID = 10308

GROUP BY InventoryItems_1.ItemID,
         InventoryItems_1.LocationID,
         InventoryItems_1.Rack,
         InventoryItems_1.Bin,
         Locations_1.LocationCode,
         Locations_1.DescriptionMed
-- HAVING InventoryItems_1.ItemID = 10308
ORDER BY InventoryItems_1.LocationID,
         MIN(InventoryItems_1.Date),
         InventoryItems_1.Rack,
         InventoryItems_1.Bin;

答案 1 :(得分:0)

您可以这样做:

SELECT 
    InventoryItems_1.ItemID
,   ISNULL(InventoryItems_1.QtyToStock, 0) AS InvQty
,   InventoryItems_1.Rack
,   InventoryItems_1.Bin
,   InventoryItems_1.LocationID
,   InventoryItems_1.Date
,   Locations_1.LocationCode
,   Locations_1.DescriptionMed
FROM (
    SELECT *, ROW_NUMBER() OVER(PARTITION BY ItemID, Bin, LocationID ORDER BY [Date] DESC) RN 
    FROM dbo.InventoryItems 
) InventoryItems_1
INNER JOIN dbo.Locations AS Locations_1 ON InventoryItems_1.LocationID = Locations_1.LocationID
WHERE
    InventoryItems_1.RN = 1
AND InventoryItems_1.OwnerDetailID IS NULL
AND InventoryItems_1.ItemID = 10308
ORDER BY InventoryItems_1.LocationID,
         InventoryItems_1.Rack,
         InventoryItems_1.Bin;