日期:2014-05-18 浏览次数:20548 次
select [字段A],[字段B],MAX([字段C]) as [字段C],max([字段D]) as [字段D] from test where [字段A] not in( select [字段A] from test where [字段B]='结束' ) group by [字段A],[字段B]
------解决方案--------------------
select a.字段A ,a.字段B,a.字段C,b.字段D from tab a,tab b where a.字段A = b.字段A and not exists ( select 1 from tab where 字段A = a.字段A and 字段B = '结束' ) and not exists ( select 1 from tab where 字段A = a.字段A and 字段c > a.字段c ) and not exists ( select 1 from tab where 字段A = b.字段A and 字段c < b.字段c )
------解决方案--------------------
IF EXISTS (SELECT 1 FROM SYSOBJECTS WHERE name = 'tba') BEGIN DROP TABLE tba END GO CREATE TABLE tba ( a VARCHAR(10), b VARCHAR(10), c INT, d VARCHAR(10) ) GO INSERT INTO tba SELECT 'A','结束',3,'a' UNION SELECT 'A','开始',2,'b' UNION SELECT 'A','开始',1,'c' UNION SELECT 'B','结束',7,'a' UNION SELECT 'B','开始',6,'b' UNION SELECT 'B','开始',5,'c' UNION SELECT 'B','开始',4,'d' UNION SELECT 'B','开始',3,'e' UNION SELECT 'B','开始',2,'f' UNION SELECT 'B','开始',1,'g' UNION SELECT 'C','开始',2,'a' UNION SELECT 'C','开始',1,'b' UNION SELECT 'D','开始',3,'a' UNION SELECT 'D','开始',2,'b' UNION SELECT 'D','开始',1,'c' SELECT A,b,MAX(c) AS C,MAX(d) AS D FROM tba WHERE a NOT IN (SELECT a from tba WHERE b = '结束') GROUP BY a,b A b C D C 开始 2 b D 开始 3 c