这是原表table1A   B   C
122 87  1
122 107 2
122 112 3
122 114 4
122 115 5
122 116 6
122 283 7
122 348 8
122 282 9
122 280 10
122 564 11
122 24  12
122 81  13
122 80  14
122 74  15
122 87  16我现在希望把a做为子查询, 过滤下,过滤条件是B为87. 
也就是说. C这一蓝的值, 都减少一个和B为87时C的值, 如果B为87时有多个值,
那么就绝对值最小的一个最终得到下列值.
A   B   C
122 87  0
122 107 1
122 112 2
122 114 3
122 115 4
122 116 5
122 283 6
122 348 7
122 282 7
122 280 6
122 564 5
122 24  4
122 81  3
122 80  2
122 74  1
122 87  0
select   a,b, abs(c-(select top 1 c from table where b=87))
from table 这样可以减少一个b=87的值, 但却不是最小值.

解决方案 »

  1.   

    补充下, a的值不一定是122, 每一组分别使用上面的规则select a, b, abs(c-(select top 1 c from table where b=87 and a=tt.a))
    from table tt
    where a in 
    (
      select a from table where b=87
    )但还是那个min搞不定
      

  2.   


    declare @a table(a int,b int,c int)
    insert @a select 122, 87, 1
    union all select 122 ,107, 2
    union all select 122, 112, 3
    union all select 122, 114, 4
    union all select 122, 115, 5
    union all select 122, 116, 6
    union all select 122, 283, 7
    union all select 122, 348, 8
    union all select 122, 282, 9
    union all select 122, 280, 10
    union all select 122, 564, 11
    union all select 122, 24, 12
    union all select 122, 81, 13
    union all select 122, 80, 14
    union all select 122, 74, 15
    union all select 122, 87, 16
    select f.a,f.b,min(g.d) d from @a f Inner Join
    (select  a,b,c,abs(c-s) d from
    (select * from @a) c cross join
    (select c s from @a where b=87) d) g on f.a=g.a and f.b=g.b group by f.a,f.b order by d
      

  3.   

    建一个有Min(C),和Count(C),并且having Count(C)>1的子查询,然后右连接原始表,条件设为子查询的任一字段为空就应该可以了.大约就是:
    select table1.a,table1.b,table1.c
    from table1
    left join (select b,Min(C) as lsc,Count(C) as lcc from table1 group by b having count(C)>1) as lsa on  lsa.b=table1.b and lsa.lsc=table1.c
    where lsa.lsc is null
      

  4.   

    不好意思,没看懂题,看成是去掉最小的值了,不太理解你的题义,不知是不是要下面的效果:
    select table1.a,table1.b,table1.c-lsa.lsc
    from table1
    left join (select b,Min(C) as lsc from table1 group by b) as lsa on  lsa.b=table1.b
      

  5.   

    按你说的应该是这个结果:
    a           b                       
    ----------- ----------- ----------- 
    122         87          0
    122         107         1
    122         112         2
    122         114         3
    122         115         4
    122         116         5
    122         283         6
    122         348         7
    122         282         8
    122         280         9
    122         564         10
    122         24          11
    122         81          12
    122         80          13
    122         74          14
    122         87          15如果是要这个结果只要:
    select a,b,abs(c-(select min(c) from @a where b=87))
    from @a那就不是难题了,楼主描述有问题????
      

  6.   

    to Yang_(扬帆破浪) :
    a字段起什么作用
    ---------------
    where a in
    (
    select a from table where b=87
    )
    选择要修改的记录用,与主题无大关系。122 282 9 怎么变成 122 282 7 的也没搞明白:(
      

  7.   

    declare @a table(a int,b int,c int)
    insert @a select 122, 87, 1
    union all select 122 ,107, 2
    union all select 122, 112, 3
    union all select 122, 114, 4
    union all select 122, 115, 5
    union all select 122, 116, 6
    union all select 122, 283, 7
    union all select 122, 348, 8
    union all select 123, 282, 9
    union all select 123, 280, 10
    union all select 123, 564, 11
    union all select 123, 24, 12
    union all select 123, 81, 13
    union all select 123, 80, 14
    union all select 123, 74, 15
    union all select 123, 87, 16
    select * from @aselect a,b,abs(c-(select min(c) from @a where b=87 and a=tt.a))
    from @a tt注意我改了一下数据,也就是把下一组a的值改成了123,这就可以有楼主要求的效果a           b           c           
    ----------- ----------- ----------- 
    122         87          1
    122         107         2
    122         112         3
    122         114         4
    122         115         5
    122         116         6
    122         283         7
    122         348         8
    123         282         9
    123         280         10
    123         564         11
    123         24          12
    123         81          13
    123         80          14
    123         74          15
    123         87          16(所影响的行数为 16 行)