这是原表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的值, 但却不是最小值.
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的值, 但却不是最小值.
解决方案 »
- sql 两张表之间的查询
- sql 语句
- sql2000 高手麻烦帮忙看一下
- 高手请看看这个DTS的ActiveX脚本是什么错误呀
- 相同班级的人只选出一个人怎么做?
- tempdb文件组满导致程序中断
- 请教MDX创建计算成员
- 马上要叫测试版了,大家帮帮忙!!!!!!!!!!!!!!
- 我在Windows 2000 上装了SQL Server 2000 企业版,用的是混合认证模式,为什么改了Win2000 后,SQL Server 的服务起不来
- 又要麻烦大家了,SQLSERVER中如何很方便的把DATATIME类型中的HOUR和MINUTE取出来
- 如果使用group by?
- 字段为nvarchar,内容里面汉字的格式为“8G74G6DG”,如何用select * from table where column
from table tt
where a in
(
select a from table where b=87
)但还是那个min搞不定
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
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
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
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那就不是难题了,楼主描述有问题????
a字段起什么作用
---------------
where a in
(
select a from table where b=87
)
选择要修改的记录用,与主题无大关系。122 282 9 怎么变成 122 282 7 的也没搞明白:(
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 行)