|
[code=sql]USE tempdb
GO
IF OBJECT_ID('TEST') IS NOT NULL
DROP TABLE TEST;
GO
CREATE TABLE TEST
(
物资编码 CHAR(8),
名称 NVARCHAR(10),
单价 MONEY,
日期 DATE,
);
GO
INSERT INTO TEST VALUES('1','A',.5,'2013-8-28');
INSERT INTO TEST VALUES('2','B',1.9,'2012-10-20');
INSERT INTO TEST VALUES('1','A',.3,'2013-7-28');
INSERT INTO TEST VALUES('2','B',2.6,'2011-4-5');
INSERT INTO TEST VALUES('3','C',5.5,'2012-4-5');
INSERT INTO TEST VALUES('3','C',4.4,'2012-10-5');
INSERT INTO TEST VALUES('1','A',.9,'2013-6-28');
INSERT INTO TEST VALUES('1','A',.1,'2013-8-20');
INSERT INTO TEST VALUES('2','B',.8,'2012-6-10');
INSERT INTO TEST VALUES('2','B',1.5,'2012-10-10');
GO
----------------------------------------
SELECT * FROM TEST AS A
WHERE 日期 IN (SELECT MIN(日期) FROM TEST AS B
WHERE A.物资编码 = B.物资编码)
UNION ALL
SELECT * FROM TEST AS A
WHERE 日期 IN (SELECT MAX(日期) FROM TEST AS B
WHERE A.物资编码 = B.物资编码)
ORDER BY 1,4
----------------------------------------
/*
物资编码 名称 单价 日期
-------- ---------- --------------------- ----------
1 A 0.90 2013-06-28
1 A 0.50 2013-08-28
2 B 2.60 2011-04-05
2 B 1.90 2012-10-20
3 C 5.50 2012-04-05
3 C 4.40 2012-10-05
*/[/code] |
|