可以变通的: http://community.csdn.net/Expert/topic/3354/3354399.xml?temp=.2987329不用distinct: select a,b from (select top 100 percent * from table1 tt where not exists(select * from table1 where c<tt.c and a=tt.a and b=tt.b) order by c desc) t
首先,你的排序条件似乎不太正确:既然对于重复的(a, b)会有多个不同的c,那么应该选取哪一个c作为排序的条件呢? 例如:create talbe #t (a int, b int, c int) go insert into #t select 1, 2, 24 union select 2, 109, 25 union select 2, 109, 23 go那么在表@t中,a = 2, b = 9的情况下,c值为23或25;在a=1, b=2的情况下, c=24。 那么在a = 2, b = 9中应该选择哪个c来排序呢?此处你未指定选取c的规则,所以当然不正确。 解决之道:指定选取c的规则,比如a,b相同的情况下,选择最小的c 如此一来,语句为: select distinct a, b from #t order by min(c) 但是, distinct要求:在后面的order by子句中的字段必须出现在选择列中(why?可以再研究一下,反正现在没有想通)。 所以最终的解是: select a,b from #t group by a, b order by min(c) 记住,distinct和group在大多数情况下是可以互换的:)此处是一个特例。--完整的可执行例子: declare @t table(a int, b int, c int) insert into @t select 1, 2, 24 union select 2, 109, 25 union select 2, 109, 24select a, b from @t group by a, b order by min(c)/* 结果集 a b ----------- ----------- 1 2 2 109 */
select distinct A,B from (select * from Table1 order by C desc) a--加别名 --或者: select A,B from table1 order by min(C) desc
--上面少了group by --或者: select A,B from table1 group by A,B order by min(C) desc
咔咔咔!结果集写错误!我一错再错!错不可恕啊! 关于distinct的解释,大家可以讨论一下、指点一下、批评一下: distinct 并没有进行分组, 不能像group那样使用聚合函数。 比如,你可以写: select a, b, min(c) from #t group by a, b 但你不能写 select distinct a, b , min(c) from #t
distinct的内部执行处理方式还是group by 所以我觉得这个问题我这样写与楼主的要求是一样的:select A,B from table1 group by A,B order by min(C) desc
select top 3000 users_bumen from users group by users_bumen order by min(users_id) desc 很管用的
http://community.csdn.net/Expert/topic/3354/3354399.xml?temp=.2987329不用distinct:
select a,b from
(select top 100 percent * from table1 tt
where not exists(select * from table1 where c<tt.c and a=tt.a and b=tt.b)
order by c desc) t
例如:create talbe #t (a int, b int, c int)
go
insert into #t
select 1, 2, 24
union
select 2, 109, 25
union
select 2, 109, 23
go那么在表@t中,a = 2, b = 9的情况下,c值为23或25;在a=1, b=2的情况下, c=24。
那么在a = 2, b = 9中应该选择哪个c来排序呢?此处你未指定选取c的规则,所以当然不正确。
解决之道:指定选取c的规则,比如a,b相同的情况下,选择最小的c
如此一来,语句为:
select distinct a, b from #t
order by min(c)
但是, distinct要求:在后面的order by子句中的字段必须出现在选择列中(why?可以再研究一下,反正现在没有想通)。
所以最终的解是:
select a,b from #t
group by a, b
order by min(c)
记住,distinct和group在大多数情况下是可以互换的:)此处是一个特例。--完整的可执行例子:
declare @t table(a int, b int, c int)
insert into @t
select 1, 2, 24
union
select 2, 109, 25
union
select 2, 109, 24select a, b from @t
group by a, b
order by min(c)/*
结果集
a b
----------- -----------
1 2
2 109
*/
--或者:
select A,B from table1 order by min(C) desc
--或者:
select A,B from table1 group by A,B order by min(C) desc
关于distinct的解释,大家可以讨论一下、指点一下、批评一下:
distinct 并没有进行分组, 不能像group那样使用聚合函数。
比如,你可以写:
select a, b, min(c) from #t
group by a, b
但你不能写
select distinct a, b , min(c) from #t
from users
group by users_bumen
order by min(users_id) desc
很管用的