首页 > 技术文章 > group by 与 order by 一起使用的时候

vichin 2017-02-02 16:23 原文

select 后面的列+order by 后面的列 
必须在group by 里面

也就是说 select 和 order by 后面的列是 group by 列的子集

而 select 和 order by 之间是没有什么瓜葛的。

 

SELECT [col1] ,[col2],MAX([col3]) FROM [tb] GROUP BY [col1] ,[col2]  ORDER BY [col1] ,[col2] ,MAX([col3])
SELECT [col1] ,[col2],MAX([col3]) AS  [col3] FROM [tb] GROUP BY [col1] ,[col2]  ORDER BY [col1] ,[col2] ,[col3]
SELECT [col1] ,[col2] FROM [tb] GROUP BY [col1] ,[col2] ,[col3] ORDER BY [col1] ,[col2] ,[col3]
select [TYPE],LAST_VERSION,REMARK FROM SMTSOPVersionInfo WHERE 
SOP=N'" + this.SOP.Value + "'group by [TYPE],LAST_VERSION,REMARK order by LAST_VERSION;

 

推荐阅读