June 16, 2012 at 10:24 am
is it possible to give an SELECT statement ...ORDER BY but the field in ORDER BY clause would not be in SELECT list.
Like ...
SELECT F1, F2, F3 from table ORDER BY F4 asc
June 16, 2012 at 11:13 am
In simple words yes it is. As an example:
SELECT [Id],[Col1], [Col2]
FROM [dbo].[QOD60] ORDER BY [Col2]
Results:
Id Col1 Col2
----------- ----------- -----------
4 4 1
1 1 2
2 2 3
3 3 4
SELECT [Id],[Col1]
FROM [dbo].[QOD60] ORDER BY [Col2]
Results:
Id Col1
----------- -----------
4 4
1 1
2 2
3 3
June 16, 2012 at 11:20 am
balasach82 (6/16/2012)
is it possible to give an SELECT statement ...ORDER BY but the field in ORDER BY clause would not be in SELECT list.Like ...
SELECT F1, F2, F3 from table ORDER BY F4 asc
Yes, you can order by a field(s)even if it is not your in your select statement but exists in your table. For a group by clause though you'd need it to be in your select statement.
June 19, 2012 at 3:03 pm
peacesells (6/16/2012)
balasach82 (6/16/2012)
is it possible to give an SELECT statement ...ORDER BY but the field in ORDER BY clause would not be in SELECT list.Like ...
SELECT F1, F2, F3 from table ORDER BY F4 asc
Yes, you can order by a field(s)even if it is not your in your select statement but exists in your table. For a group by clause though you'd need it to be in your select statement.
There's another exception, when you're using SELECT DISTINCT you must include the fields used in the GROUP BY clause in the select list.
June 19, 2012 at 3:14 pm
Luis Cazares (6/19/2012)
peacesells (6/16/2012)
balasach82 (6/16/2012)
is it possible to give an SELECT statement ...ORDER BY but the field in ORDER BY clause would not be in SELECT list.Like ...
SELECT F1, F2, F3 from table ORDER BY F4 asc
Yes, you can order by a field(s)even if it is not your in your select statement but exists in your table. For a group by clause though you'd need it to be in your select statement.
There's another exception, when you're using SELECT DISTINCT you must include the fields used in the GROUP BY clause in the select list.
Why is this in addition to
For a group by clause though you'd need it to be in your select statement.
?
Jared
CE - Microsoft
June 19, 2012 at 7:00 pm
June 20, 2012 at 3:17 am
If you're SELECTing from the results delivered by a CTE, any field listed on the ORDER BY must also be returned from the CTE, even if they're not part of the SELECT list FROM CTE.
Just thought I'd mention it. A CTE isn't really different than a table in this respect.
My thought question: Have you ever been told that your query runs too fast?
My advice:
INDEXing a poor-performing query is like putting sugar on cat food. Yeah, it probably tastes better but are you sure you want to eat it?
The path of least resistance can be a slippery slope. Take care that fixing your fixes of fixes doesn't snowball and end up costing you more than fixing the root cause would have in the first place.
Need to UNPIVOT? Why not CROSS APPLY VALUES instead?[/url]
Since random numbers are too important to be left to chance, let's generate some![/url]
Learn to understand recursive CTEs by example.[/url]
[url url=http://www.sqlservercentral.com/articles/St
Viewing 7 posts - 1 through 6 (of 6 total)
You must be logged in to reply to this topic. Login to reply