0
     DateTime        --  Unit  --  Client  --- Qty
03/02/2013 08:00:01  --   3     --   1      ---   1
03/02/2013 08:00:02  --   3     --   2      ---   1
03/02/2013 08:00:03  --   3     --   3      ---   2
03/02/2013 08:00:04  --   3     --   3      ---   2
03/02/2013 08:00:05  --   3     --   3      ---   5
03/02/2013 08:00:06  --   3     --   3      ---   4
03/02/2013 08:00:07  --   3     --   4      ---   6
03/02/2013 08:00:08  --   3     --   4      ---   67
03/02/2013 08:00:09  --   3     --   4      ---   76
03/02/2013 08:00:10  --   3     --   4      ---   76

而且我要 :

     DateTime        --  Unit  --  Client  --- Qty
03/02/2013 08:00:01  --   3     --   1      ---   1
03/02/2013 08:00:02  --   3     --   2      ---   1
03/02/2013 08:00:03  --   3     --   3      ---   2
03/02/2013 08:00:05  --   3     --   3      ---   5
03/02/2013 08:00:07  --   3     --   4      ---   6
03/02/2013 08:00:09  --   3     --   4      ---   76

过滤的标准是从表中获取最小和最大“数量”,当在同一“单位”和“客户”列中存在重复的“数量”值时,只获取第一个值。

我是按照T-SQL做的,但是当“Unit”和“client”列相同时,检索的是最后一个“Qty”值,我需要第一个。

    --1
CREATE TABLE Transact
    (DateTime   DateTime,
     Unit       INT NULL,
     Client     INT NULL,
     Qty        INT NULL
    )

INSERT INTO Transact (Datetime,Unit,Client,Qty)
    Values ( '03/02/2013 08:00:01',3,1,1)
    Values ( '03/02/2013 08:00:02',3,2,1)
    Values ( '03/02/2013 08:00:03',3,3,2)
    Values ( '03/02/2013 08:00:04',3,3,2)
    Values ( '03/02/2013 08:00:05',3,3,5)
    Values ( '03/02/2013 08:00:06',3,3,4)
    Values ( '03/02/2013 08:00:07',3,4,6)
    Values ( '03/02/2013 08:00:08',3,4,67)
    Values ( '03/02/2013 08:00:09',3,4,76)
    Values ( '03/02/2013 08:00:10',3,4,76)


DECLARE @Total TABLE
    (DateTime   DateTime,
     Unit       INT NULL,
     Client     INT NULL,
     Qty        INT NULL
    )
DECLARE @Uniques TABLE
    (DateTime   DateTime,
     Unit       INT NULL,
     Client     INT NULL,
     Qty        INT NULL
    )
DECLARE @Mini TABLE
    (DateTime   DateTime,
     Unit       INT NULL,
     Client     INT NULL,
     Qty        INT NULL
    )
DECLARE @Maxi TABLE
    (DateTime   DateTime,
     Unit       INT NULL,
     Client     INT NULL,
     Qty        INT NULL
    )
--2

INSERT INTO @Total SELECT * FROM Transact

INSERT INTO @Mini SELECT  MIN(Datetime) Datetime,Unit,Client,MIN(Qty) FROM @Total GROUP BY Unit,Client 
INSERT INTO @Maxi SELECT  MAX(Datetime) Datetime,Unit,Client,MAX(Qty) FROM @Total GROUP BY Unit,Client

--3

INSERT INTO @Uniques SELECT * FROM @Mini UNION SELECT * FROM @Maxi

SELECT * FROM @Uniques

提前致谢。

巴勃罗·杰罗尼莫。

4

3 回答 3

3

试试这个:

WITH MinCTE
AS
(
  SELECT *,
    ROW_NUMBER() OVER(Unit, PARTITION BY Client
                      ORDER BY Qty, DateTime ) AS RN
  FROM Transact
), MaxCTE
AS
(
  SELECT *,
    ROW_NUMBER() OVER(PARTITION BY Unit, Client
                      ORDER BY Qty DESC, DateTime) AS RN
  FROM Transact
)
SELECT DateTime, Unit, Client, Qty FROM MinCTE WhERE RN = 1
UNION  
SELECT DateTime, Unit, Client, Qty FROM MaxCTE WhERE RN = 1;
于 2013-03-17T02:21:03.323 回答
1

以下是您可以尝试的方法:

SELECT DISTINCT MIN(DateTime), MIN(Qty), Unit, Client FROM Transact
GROUP BY Unit, Client
UNION
SELECT DISTINCT MAX(DateTime), MAX(Qty), Unit, Client FROM Transact
GROUP BY Unit, Client
于 2013-03-17T06:17:08.170 回答
0

首先将具有 min 和 max Qty 的记录选择到单个表中,然后为这些记录选择 min DateTime。

SELECT MIN(DateTime), [Transact].Unit, [Transact].Client, [Transact].Qty 
FROM (
    SELECT DISTINCT Unit, Client, MIN(Qty) as Qty
    FROM [Transact]
    GROUP BY Unit, Client
    UNION
    SELECT Unit, Client, MAX(Qty)
    FROM [Transact]
    GROUP BY Unit, Client
    ) as MinMax
JOIN [Transact] ON [Transact].Unit = MinMax.Unit AND [Transact].Client = MinMax.Client AND [Transact].Qty = MinMax.Qty
GROUP BY [Transact].Unit, [Transact].Client, [Transact].Qty

我使用了一个子查询,但如果您愿意,不难将其更改为内存表。

于 2013-03-17T02:40:50.543 回答