当前位置: 代码迷 >> Sql Server >> 小弟我想用某一个字段a排序,但是小弟我的SQL语句包含distinct,在distinct的时候不想把字段a给列进去
  详细解决方案

小弟我想用某一个字段a排序,但是小弟我的SQL语句包含distinct,在distinct的时候不想把字段a给列进去

热度:68   发布时间:2016-04-27 14:13:15.0
我想用某一个字段a排序,但是我的SQL语句包含distinct,在distinct的时候不想把字段a给列进去
sqlserver:SQL语句
我想用某一个字段a排序,也就是order by a
但是我的SQL语句包含distinct,在distinct的时候不想把字段a给列进去。
应该怎么实现。
语句如下:
select distinct b,c from table order by a desc(这样肯定会报错的,因为a在order by 里,而且语句还包含distince,a就必须出现在select中)

------解决方案--------------------
SELECT B,C FROM TB GROUP BY B,C ORDER BY MIN(A)

TRY
------解决方案--------------------
SQL code
select b,c,min(a) a from tbgroup by b,corder by a desc
------解决方案--------------------
SQL code
declare @t table (b int,c int,a int)insert into @tselect 1,1,4 union allselect 1,1,2 union allselect 2,2,3 union allselect 3,3,6 union allselect 2,2,5 union allselect 3,3,1SELECT distinct b,c from @t/*b           c----------- -----------1           12           23           3*/
  相关解决方案