再来一个子查询。麻烦大家了。
字段A字段B字段C
A结束3
A开始2
A开始1
B结束7
B开始6
B开始5
B开始4
B开始3
B开始2
B开始1
C开始2
C开始1
D开始3
D开始2
D开始1
字段B是字段A的装填,需要把不含结束的记录集提取出来,并且只去最大的那个,所以,最终结果就是:
C 开始 2
D 开始 3
这样一个查询怎么写呢?
[解决办法]
- SQL code
--> 测试数据:[test]if object_id('[test]') is not null drop table [test]create table [test]([字段A] varchar(1),[字段B] varchar(4),[字段C] int)insert [test]select 'A','结束',3 union allselect 'A','开始',2 union allselect 'A','开始',1 union allselect 'B','结束',7 union allselect 'B','开始',6 union allselect 'B','开始',5 union allselect 'B','开始',4 union allselect 'B','开始',3 union allselect 'B','开始',2 union allselect 'B','开始',1 union allselect 'C','开始',2 union allselect 'C','开始',1 union allselect 'D','开始',3 union allselect 'D','开始',2 union allselect 'D','开始',1select [字段A],[字段B],MAX([字段C]) as [字段C]from testwhere [字段A] not in(select [字段A] from test where [字段B]='结束')group by [字段A],[字段B]/*字段A 字段B 字段CC 开始 2D 开始 3*/