22,209
社区成员
发帖
与我相关
我的任务
分享
declare @t table (b int,c int,a int)
insert into @t
select 1,1,4 union all
select 1,1,2 union all
select 2,2,3 union all
select 3,3,6 union all
select 2,2,5 union all
select 3,3,1
--按相同b,c对应的最小a排序
SELECT b,c from @t GROUP BY b,c ORDER BY MIN(a)
/*
b c
----------- -----------
3 3
1 1
2 2
*/
--按相同b,c对应的最大a排序
SELECT b,c from @t GROUP BY b,c ORDER BY max(a)
/*
b c
----------- -----------
1 1
2 2
3 3
*/
--按相同b,c对应的平均a排序
SELECT b,c from @t GROUP BY b,c ORDER BY avg(a)
/*
b c
----------- -----------
1 1
3 3
2 2
*/
declare @t table (b int,c int,a int)
insert into @t
select 1,1,4 union all
select 1,1,2 union all
select 2,2,3 union all
select 3,3,6 union all
select 2,2,5 union all
select 3,3,1
SELECT distinct b,c from @t
/*
b c
----------- -----------
1 1
2 2
3 3
*/
select b,c,min(a) a
from tb
group by b,c
order by a desc