问个sql查询问题。。。。。。
有一个表A
dot1 dot2 distance
1 2 743
1 3 3749
。 。 。
。 。 。
2 1 743
2 3 372
。 。 。
。 。 。
现在要求dot1中相同的情况下distance的最小值,也就是有dot1=1时最小的distance,有dot1=2时最小的distance,是能帮忙写下sql语句,要求字段是dot1,dot2,distance(最小值)
问题点数:0、回复次数:16Top
1 楼sbw(清风一笑)回复于 2004-11-01 20:33:22 得分 0
select dot1,dot2,distance from a group by dot1 order by distance descTop
2 楼leungzhq2000(月下独茁)回复于 2004-11-01 21:34:56 得分 0
可以使用多个语句来进行吗?
如果可以,你可以参考一下这个:(最好在程序中使用了)
sele distinct dot1 from A order by dot1
count to num1
dimension arr1(num1,1)
copy to array arr1
for i=1 to num1
sele * from A where dot1=arr1(i,1) order by distance nowait
count to num2
dimension arr2(num2,3)
copy to array arr2
?arr2(i,1),arr2(i,2),arr2(i,3)
next
这样就能把你想要的结果显示出来。这样你可以解决你想要的东西,比如表形式的结果,其他等等!~
Top
3 楼itzhiren(itzhiren)回复于 2004-11-02 08:22:43 得分 0
select dot1,dot2,distance from a group by dot1 order by distanceTop
4 楼Surpass((本账户已更换主人))回复于 2004-11-02 09:15:08 得分 0
SELECT dot1,dot2,distance FROM A GROUP BY dot1 ORDER BY distance
Top
5 楼xclzteb(浪子)回复于 2004-11-02 09:39:04 得分 0
select tab2.dot1,tab1.dot2,tab2.distance From a as tab1,(select dot1,Min(distance) From a group by dot1 order by dot1) as tab2 where tab1.dot1=tab2.dot1 and tab1.distance=tab2.distanceTop
6 楼xclzteb(浪子)回复于 2004-11-02 09:40:13 得分 0
这就是你想要的结果,你也可以指定另外的列名。Top
7 楼JohnShen(因果网)回复于 2004-11-02 09:41:08 得分 0
没有测试,你先试试:
SELECT dot1,min(distance) as min_distance FROM A into cursor cTemp GROUP BY dot1
Select A.dot1,A.dot2,A.distance From A ;
Left Join cTemp On A.dot1 = cTemp.dot1 And A.distance = cTemp.min_distance;
Where Not(Isnull(A.dot1) Or Empty(A.dot1))Top
8 楼xclzteb(浪子)回复于 2004-11-02 09:52:49 得分 0
在VFP中用“因果网”的能行,如果是SQL语句就用我给出的那个,经过类似表进行了调试通过。Top
9 楼xclzteb(浪子)回复于 2004-11-02 09:53:44 得分 0
“因果网”的用到了临时表Top
10 楼LAIYANGPJ(小丑)回复于 2004-11-02 11:49:23 得分 0
sele distinct x.dot1 as dot1,x.dot2 as dot2,x.distance as distance from a x,a y where x.dot1=y.dot1 and x.distance<=all(sele distance from a where dot1=x.dot1)
Top
11 楼fengyeng(在城里飘~~~~)回复于 2004-11-09 14:59:42 得分 0
同意楼上!
Top
12 楼davorsuker39(大狐狸)回复于 2004-11-09 15:32:51 得分 0
select distinct a.dot1,a.dot2 ,a.distance from A a, where (select * from A where dot1=a.dot1 and a.distance<=all(select distance from a where dot1=a.dot1))Top
13 楼fengyeng(在城里飘~~~~)回复于 2004-11-12 15:01:35 得分 0
select tab2.dot1,tab1.dot2,tab2.distance From a as tab1,(select dot1,Min(distance) From a group by dot1 order by dot1) as tab2 where tab1.dot1=tab2.dot1 and tab1.distance=tab2.distance
Top
14 楼mh_xxw(毛毛)回复于 2004-11-19 02:13:50 得分 0
SELECT dot1,dot2,distance FROM A GROUP BY dot1 ORDER BY distanceTop
15 楼mh_xxw(毛毛)回复于 2004-11-19 02:14:50 得分 0
另,二楼的回答较好Top
16 楼leungzhq2000(月下独茁)回复于 2004-11-21 19:21:21 得分 0
回来看一下,还没有给我加分啊 !~一定是我回答的不好了!!唉,还得努力了!!!
mh_xxw(毛毛) ( ) 谢谢!你应该是再说我的回答吧!这是我第一次接倒别人的表扬!
我会努力的!
我很无聊,!是吧??
呵呵,今天没有事情做,来这里看看!!Top




