日期:2014-05-18  浏览次数:20413 次

SQL ,鸟查询。问题很简单,就是想不出来
SQL code

id          a           b           c
----------- ----------- ----------- -----------
1           2           3           4
1           3           5           4
1           3           7           4
1           3           7           8

2           2           3           4
2           5           3           8
2           5           3           8
2           7           3           8
2           1           9           9

(9 行受影响)




抓出除最大值以外的数据。就是把每个字段的最大值拿掉,按ID分组,也就是说A列有两个最大值。3和7 以此类推

SQL code

create table #test (id int, a int, b int , c int)
insert #test select 1,2,3,4 union all 
select 1,3,5,4 union all
select 1,3,7,4 union all
select 1,3,7,8 union all 
select 2,2,3,4 union all 
select 2,5,3,8 union all 
select 2,5,3,8 union all 
select 2,7,3,8 union all
select 2,1,9,9



源码

------解决方案--------------------
变态的需求,结贴给分吧
------解决方案--------------------
SQL code

create table tLife
(
id int,
a int,
b int,
c int
)

insert into tLife 
select 1        ,   2,           3,           4 union all
select 1        ,   3,           5,           4 union all
select 1        ,   3,           7,           4 union all
select 1        ,   3,           7,           8 union all
select 2        ,   2,           3,           4 union all
select 2        ,   5,           3,           8 union all
select 2        ,   5,           3,           8 union all
select 2        ,   7,           3,           8 union all
select 2        ,   1,           9,           9
go

select * from tLife
except
select *
from tLife t
where not exists (select 1 from tLife where id = t.id and a+b+c > t.a+t.b+t.c)

drop table tLife

/********************

id          a           b           c
----------- ----------- ----------- -----------
1           2           3           4
1           3           5           4
1           3           7           4
2           2           3           4
2           5           3           8
2           7           3           8

(6 行受影响)

------解决方案--------------------
SQL code
select A.*
from #test A
inner join (select id,max(a) as a from #test group by id) T on A.a <> T.a and A.id =T.id
inner join (select id,max(b) as b from #test group by id) T1 on A.b <> T1.b and A.id =T1.id
inner join (select id,max(c) as c from #test group by id) T2 on A.c <> T2.c and A.id =T2.id


/*
1    2    3    4
2    2    3    4
2    5    3    8
2    5    3    8*/