Table1:区域表
TypeId TypeName 
1  厦门 
2  上海 
3  陕西 
Table2:服务器表
ServerId    TypeId     Power Level 
1  1  10  8 
2  1  8  7 
3  2   5  9 
4  1   6  2 
5  3   9  1 表1和表2通过TypeId关联
现在要选择Table1中的TypeId和TypeName并且与它关联的Table2中的对应的Power要大于3,最后得到的结果按Level降序来排,并且得到的Table1中的记录不能为重复的。如果使用下面的SQL语句:(加上distinct后会出错)
Select A.TypeId, A.TypeName From Table1 AS A, Table2 AS B Where A.TypeId = B.TypeId and B.power > 3 order by B.Level DESC 得到的结果是:
2|上海
1|厦门
1|厦门
1|厦门
3|陕西
----
而我想得到的结果是:
2|上海
1|厦门
3|陕西请问这条SQL语句应该怎么写呢?asp 测试文件和数据库
http://www.900t.com/temp/testfile.rar

解决方案 »

  1.   

    select AB.* from (Select A.TypeId, A.TypeName From Table1 AS A, Table2 AS B Where A.TypeId = B.TypeId and B.power > 3 order by B.Level DESC )AB  group by AB.TypeId, AB.TypeName
      

  2.   

    Select A.TypeId, A.TypeName From Table1 AS A, Table2 AS B Where A.TypeId = B.TypeId and B.power > 3 
    Gruop by AB.TypeId,AB.TypeName
    order by B.Level DESC
      

  3.   

    Select A.TypeId, A.TypeName From Table1 AS A, Table2 AS B Where A.TypeId = B.TypeId and B.power > 3 
    Gruop by A.TypeId,A.TypeName
    order by B.Level DESC
      

  4.   

    在大家的帮助下,终于搞定了,
    我之前重要的是没有考虑到用group by来避免重复的记录
    Select A.TypeId, A.TypeName From Table1 AS A, Table2 AS B Where A.TypeId = B.TypeId  group by A.TypeId, A.TypeName order by max(B.Level) DESC我这里level是数字型的,所以可以用max()函数的到处理.非常感谢前几位老兄的帮助.