ID-----B-------M------D-------A
86-----2------104----2210----2193
86-----2------201----1618----1618
86-----2------104----2247----12
86-----2------201----1650----183
86-----2------104----4465----4465
86-----2------201----3118----183
86-----2------502----7.14----7.11
86-----2------301----520-----520.38
86-----2------401----130-----129.75
86-----2------601----309.75--310
已知 2230 这个数
怎么才能用最接近来找出以下这条数据
(D要求最接近2230并且 最小值((A-D)/D ))
得到以下的整条数据
86-----2------104----2210----2193
86-----2------104----2210----2193
86-----2------201----1618----1618
86-----2------104----2247----12
86-----2------201----1650----183
86-----2------104----4465----4465
86-----2------201----3118----183
86-----2------502----7.14----7.11
86-----2------301----520-----520.38
86-----2------401----130-----129.75
86-----2------601----309.75--310
已知 2230 这个数
怎么才能用最接近来找出以下这条数据
(D要求最接近2230并且 最小值((A-D)/D ))
得到以下的整条数据
86-----2------104----2210----2193
WHERE ID=86 AND M='104'
另外,你的条件有问题。最接近2230和min((a-d)/d)哪个优先。
大致就是这样的意思,但是这条语句是查不到的,不知道怎么修改
SELECT * FROM 表 WHERE ID=86 AND M='104' AND (SELECT min(abs(D-2230)) FROM 表 WHERE ID=86 AND M='104') AND (SELECT MIN(ABS((A-D)/D)) FROM 表 WHERE ID=86 AND M='104')
SELECT TOP 1 ID,B,M,D,A FROM (SELECT *,ABS(D-2230) ,ABS((A-D)/D) FROM 数据表 ORDER BY ABS(D-2230),ABS((A-D)/D))结果应该为:86-----2------104----2247----12
不可能是86-----2------104----2210----2193如果你认为正确的结果是86-----2------104----2210----2193,那说明你的需求本身有问题,你可以通过SELECT *,ABS(D-2230) ,ABS((A-D)/D) FROM 数据表 ORDER BY ABS(D-2230),ABS((A-D)/D)分析一下结果。
select * from a1 where a=(
select case when min(abs(d-2230)) and max(a/d) then a else 0 end from a1)
and d=(
select case when min(abs(d-2230)) and max(a/d) then d else 0 end from a1)
and id=86 and m=104
SELECT TOP 1 * FROM (SELECT TOP 2 * FROM 数据表 ORDER BY ABS(D-2230),ABS((A-D)/D)) ORDER BY ABS((A-D)/D) ASC
ID------- B-------- M-------- D-------- A
196------1-------- 103------ 1080----- 1068
196------ 1-------- 104------ 1080----- 1097
196------ 1-------- 201------ 1566----- 1559
196------ 1-------- 103------ 1092----- 1092
196------ 1-------- 104------ 1063----- 1063
196------ 1-------- 201------ 1573----- 1573
196------ 1-------- 103------ 1080----- 1080
196------ 1-------- 104------ 1080----- 1080
196------ 1-------- 201------ 1566----- 1566
196------ 1-------- 502------ 8.98----- 8.97
196------ 1-------- 601------ 254------ 254
196------ 1-------- 402------ 80------- 79.25
196------ 1-------- 301------ 650------ 650
196------ 1-------- 401------ 86------- 85.88
select * form 表 where D 最接近 1080 and (A-D)/D 值最小的一个数 and ID=196 and B=103得到
196------ 1-------- 103------ 1080----- 1080
这一组就不能筛选,用 TOP 没有意义啊
select case when min(abs(d-2230)) and max(a/d) then a else 0 end from a1)
and d=(
select case when min(abs(d-2230)) and max(a/d) then d else 0 end from a1)
and id=86 and m=104
你这种需求只能用TOP实现,至于CASE WHEN THEN子句,ACCESS不支持。
SQL语句:
create table tb(ID int,B int,M int,D decimal(8,2),A decimal(8,2))
insert into tb select 86,2,104,2210,2193
insert into tb select 86,2,201,1618,1618
insert into tb select 86,2,104,2247,12
insert into tb select 86,2,201,1650,183
insert into tb select 86,2,104,4465,4465
insert into tb select 86,2,201,3118,183
insert into tb select 86,2,502,7.14,7.11
insert into tb select 86,2,301,520,520.38
insert into tb select 86,2,401,130,129.75
insert into tb select 86,2,601,309.75,310
select top 1 id,B,M,D,A from tb order by abs(D-2230),abs(D-A)
go
drop table tb
/*
id B M D A
----------- ----------- ----------- --------------------------------------- ---------------------------------------
86 2 104 2247.00 12.00(1 行受影响)
*/2247与2230相差17,2210与2230相差20,谁更接近呢?
create table tb(ID int,B int,M int,D decimal(8,2),A decimal(8,2))
insert into tb select 86,2,104,2210,2193
insert into tb select 86,2,201,1618,1618
insert into tb select 86,2,104,2247,12
insert into tb select 86,2,201,1650,183
insert into tb select 86,2,104,4465,4465
insert into tb select 86,2,201,3118,183
insert into tb select 86,2,502,7.14,7.11
insert into tb select 86,2,301,520,520.38
insert into tb select 86,2,401,130,129.75
insert into tb select 86,2,601,309.75,310
select top 1 id,B,M,D,A from tb order by abs(D-A),abs(D-2230)
go
drop table tb
/*
id B M D A
----------- ----------- ----------- --------------------------------------- ---------------------------------------
86 2 201 1618.00 1618.00(1 行受影响)
*/
create table tb(ID int,B int,M int,D decimal(8,2),A decimal(8,2))
insert into tb select 86,2,104,2210,2193
insert into tb select 86,2,201,1618,1618
insert into tb select 86,2,104,2247,12
insert into tb select 86,2,201,1650,183
insert into tb select 86,2,104,4465,4465
insert into tb select 86,2,201,3118,183
insert into tb select 86,2,502,7.14,7.11
insert into tb select 86,2,301,520,520.38
insert into tb select 86,2,401,130,129.75
insert into tb select 86,2,601,309.75,310
select top 1 id,B,M,D,A from tb order by abs(D-A)+abs(D-2230)
go
drop table tb
/*
id B M D A
----------- ----------- ----------- --------------------------------------- ---------------------------------------
86 2 104 2210.00 2193.00(1 行受影响)
*/
比如
表
ID------- B-------- M-------- D-------- A
196------1-------- 103------ 1080----- 1068
196------1-------- 103------ 1092----- 1092
196------1-------- 103------ 1080----- 1080196------1-------- 104------ 1080----- 108
196------1-------- 104------ 1063----- 1063
196------1-------- 104------ 1080----- 1097
这一组 去匹配得到的1080(两个数都是)
(分开求啊)最终就是
196------1-------- 103------ 1080----- 1080196------1-------- 104------ 1080----- 1097
分析:
得到的数是1080
1、那么
196------1-------- 103------ 1080----- 1068
196------1-------- 103------ 1092----- 1092
196------1-------- 103------ 1080----- 1080
第一个条件
剩下的是196------1-------- 103------ 1080----- 1068
196------1-------- 103------ 1080----- 1080再选第二个条件
(A-D)/D 绝对值最小 那肯定196------1-------- 103------ 1080----- 1080
被留下2、那么第二个196------1-------- 104------ 1080----- 108
196------1-------- 104------ 1063----- 1063
196------1-------- 104------ 1080----- 1097和1080最接近的肯定是
196------1-------- 104------ 1080----- 108
196------1-------- 104------ 1080----- 1097再选第二个条件
(A-D)/D 绝对值最小 那肯定
196------1-------- 104------ 1080----- 1097
被留下当然啊D 和 A 都是 不一定会等于得到的数的,有可能大有可能小 有可能等于
select top 1 from table1 order by ABS((A-D)/D)
狼头大哥
那个得到的数字一样要用上去的 那个就是标准数字,如果最接近并且 ABS((A-D)/D)
最小 那就是唯一的标准答案
我刚刚试了一下大哥的
ID-----B-------M------D-------A
86-----2------104----2210----2193
86-----2------104----2247----12
86-----2------104----4465----4465标准答案应该是
86-----2------104----2210----2193
这一条
结果查出来的是
86-----2------104----4465----4465196------1--------104------ 2400----- 1097
196------1--------103-------1080------1080
196------1--------103-------1092------1092应该是
196------1--------103-------1080------1080
现在出来两条
196------1--------103-------1080------1080
196------1--------103-------1092------1092所以还是有点问题 那个标准数字 要用上去的,麻烦大哥了
应该可以了,这个是正确答案,结贴大哥进下面的页面也发一下吧,250分都是你的了 http://topic.csdn.net/u/20101119/22/2e4927c5-2f16-4b87-8817-d77fa4f8a5aa.html?35281
http://topic.csdn.net/u/20101120/12/4e17f3da-d6a4-4363-a059-07f8d29a3857.html