简体   繁体   中英

Hide Order By Column From SELECT Without Using Temp Table

Well, I know we can hide order by column from select statement by using temp tables . Just wondering how to do the same without using temp tables ?

As the db I'm working is so huge, adding temp tables impact query performance a lot.

My Query is something like following:

SELECT DISTINCT
  Col1,
  MAX(Col2),
  MAX(Col3),
  Col4
FROM 
  Table1
Group By
  Col4,
  Col1
Order By
  Col4

Now all i want is to get rid of Col4 from Output without using temp tables. Can't even do that using inner query. Thanks.

If your Col4 is computed/derived and it has to be in original select, you may simple make an outer query:

SELECT G.Col1, G.C2, G.C3 from
(SELECT DISTINCT
  Col1,
  MAX(Col2) AS C2,
  MAX(Col3) AS C3,
  Col4
FROM 
  Table1
Group By
  Col4,
  Col1
Order By
  Col4) AS G

Typically, the sort column(s) does not need to be included in the select list. This is a common operation - no explicit temp table required.

select color
from fruit
order by weight

http://sqlfiddle.com/#!6/ba09d/3

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM