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
提前致谢。
巴勃罗·杰罗尼莫。