表:Test(字段:S/F/P/G)
S F P G
1 0.9 0.85 0.9 1
2 0.8 0.92 0.9 1
3 0.8 0.9 0.8 1
4 0.9 0.9 0.9 1
5 0.88 0.85 0.9 1
6 0.88 0.85 0.85 1 如何实现:
当G=1的时候:
第1行选取(因为0.85是s,f,p字段中最小的)
第2行不选取(因为0.92不是s,f,p字段中最小的)
第3行不选取(因为0.9不是s,f,p字段中最小的)
第4行选取(因为0.9是s,f,p字段中最小的)
第5行选取(因为0.85是s,f,p字段中最小的)
第6行选取(因为0.85是s,f,p字段中最小的)
S F P G
1 0.9 0.85 0.9 1
2 0.8 0.92 0.9 1
3 0.8 0.9 0.8 1
4 0.9 0.9 0.9 1
5 0.88 0.85 0.9 1
6 0.88 0.85 0.85 1 如何实现:
当G=1的时候:
第1行选取(因为0.85是s,f,p字段中最小的)
第2行不选取(因为0.92不是s,f,p字段中最小的)
第3行不选取(因为0.9不是s,f,p字段中最小的)
第4行选取(因为0.9是s,f,p字段中最小的)
第5行选取(因为0.85是s,f,p字段中最小的)
第6行选取(因为0.85是s,f,p字段中最小的)
--drop table test
insert into test select
0.9 ,0.85 , 0.9 , 1 union select
0.8 ,0.92, 0.9 , 1 union select
0.8 ,0.9 , 0.8 , 1 union select
0.9 ,0.9 , 0.9 , 1 union select
0.88 ,0.85, 0.9 , 1 union select
0.88 ,0.85, 0.85 , 1 select (case when s=(select min(t.a) from (select s as a union select f union select p) t)and s<>(select max(t.a) from (select s as a union select f union select p) t) then null else s end) as s,
(case when f=(select min(t.a) from (select s as a union select f union select p) t)and f<>(select max(t.a) from (select s as a union select f union select p) t) then null else f end) as f ,
(case when p=(select min(t.a) from (select s as a union select f union select p) t)and p<>(select max(t.a) from (select s as a union select f union select p) t) then null else p end) as p ,G from test
where g=1
s f p G
------------ ------------ ------------ ------------
NULL .90 NULL 1.00
NULL .92 .90 1.00
.88 NULL NULL 1.00
.88 NULL .90 1.00
.90 NULL .90 1.00
.90 .90 .90 1.00(所影响的行数为 6 行)
where G=1
and F>=S and F<=P
where G=1
and ((F>=S and F<=P) or (F<=S and F>=P))或
select * from test
where G=1
and ((F between S and P)
or (F between P and S))
insert into @t
select 0.9, 0.85, 0.9, 1
union all select 0.8, 0.92, 0.9, 1
union all select 0.8, 0.9, 0.8, 1
union all select 0.9, 0.9, 0.9, 1
union all select 0.88, 0.85, 0.9, 1
union all select 0.88, 0.85, 0.85, 1 select * from @t a
where exists(select 1 from @t where a.f=f and f<=a.s and f<=a.p)
and g=1
select * from test
where G=1
and F<=S and F<=P
可能我的表达意思不清晰,我实际想要的效果是:
行 公司 Col3 Col1 Col0 结果
--------------------------------------------------
1--A 0.9 0.85 0.9 1
2--B 0.8 0.92 0.9 3
3--A 0.8 0.9 0.8 1
4--C 0.9 0.9 0.9 1
5--D 0.88 0.85 0.9 1
6--F 0.88 0.85 0.85 3 执行 SQL语句后 实际返回:行 公司 Col3 Col1 Col0 结果
--------------------------------------------------
1--A 0.9 0.85 0.9 1(要、因为结果为1,对照全部3列(310)中最小) 2--B 0.8 0.92 0.9 3 (要、因为结果为3,对照全部3列(310)中最小) 3--(因为结果为1,对照 列3/1/0 来说,0.8/0.9/0.8 中 Col1列的值为全部3列(3/1/0)中最大的,不要)4--C 0.9 0.9 0.9 1 (要、因为结果为1,对照全部3列(310)中最小,也最大,但是符合,所以要)
5--D 0.88 0.85 0.9 1 (要、因为结果为1,对照全部3列(310)中最小)
6--(因为结果为3,对照 列3/1/0 来说,0.88/0.85/0.85 中 Col3列的值为全部3列(3/1/0)中最大的,不要)
请大家看看这个SQL语句怎么写!谢谢!
declare @t table(公司 varchar(10),Col3 numeric(10,2),Col1 numeric(10,2), Col0 numeric(10,2), 结果 int)
insert into @t
select 'A',0.9, 0.85, 0.9, 1
union all select 'B', 0.8, 0.92, 0.9, 3
union all select 'C', 0.8, 0.9, 0.8, 1
union all select 'D', 0.9, 0.9, 0.9, 1
union all select 'E',0.88, 0.85, 0.9, 1
union all select 'F',0.88, 0.85, 0.85, 3
--测试语句
select * from @t a
where
(exists(select 1 from @t where a.col3=col3 and col3<=a.col1 and col3<=a.col0)and 结果=3) or
(exists(select 1 from @t where a.col1=col1 and col1<=a.col3 and col1<=a.col0)and 结果=1) or
(exists(select 1 from @t where a.col0=col0 and col0<=a.col3 and col0<=a.col1)and 结果=0)如果字段一样的话,把其中的@t换成你的表名就可以了
g=1 and (f<p and f<s)自己看了一下,这样不就行?选取出了1的最小值!问题又有了,大家请看!
1--A 0.9 0.85 0.9 1
2--B 0.8 0.92 0.9 3
3--A 0.8 0.9 0.8 1
4--C 0.9 0.9 0.9 1
5--D 0.88 0.85 0.9 1
6--F 0.88 0.85 0.85 3
7--G 0.88 0.85 0.93 3
行 公司 Col3 Col1 Col0 结果
--------------------------------------------------
按照上面说的,第7条应该不选取,因为0.85最小,但是,我现在想选取3个列(3/1/0)中2个值(最小,次小)呢?就是说第7条也要选取!
7--G 0.88 0.85 0.93 1(因为1应该对应col1中的最小,但是0.88是第2小的,所以也要选取!)
第7条中
第2小值(次值),也可能出现在col3,也可能出现col0,就是他不是固定哪个的,只是 3个列中 次小的!
.80 .90 .80 3.00
.80 .92 .90 1.00
.88 .85 .85 3.00
.88 .85 .90 1.00
.90 .85 .90 1.00
.90 .90 .90 1.00
.88 .90 .93 1.00SQL:select s,f,p ,g,(select max(t.a) as te from (select s as a union select f union select p ) t ) as [max] from test
where
(g=1 and ( (select max(t.a) as te from (select s as a union select f union select p ) t )<>f or (s=f and s=p) ))执行后返回
.88 .85 .90 1.00 .90
.90 .85 .90 1.00 .90
.90 .90 .90 1.00 .90
.88 .90 .93 1.00 .93正是我想要的结果!不知道大家还有更好的\更快的 写法没有!