T-SQL如何从重复值中选择表中的min,max和first行

时间:2013-03-17 01:58:40

标签: sql sql-server tsql duplicates

     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,但当“单位”和“客户”列相同时,检索是最后一个“数量”值,我需要第一个。

    --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

提前致谢。

Pablo Geronimo。

3 个答案:

答案 0 :(得分: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;

答案 1 :(得分: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

答案 2 :(得分:0)

首先选择具有最小和最大数量的记录到单个表,然后为这些记录选择最小日期时间。

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

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