Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 Dynamically created column and sort

Author  Topic 

nietzky
Yak Posting Veteran

75 Posts

Posted - 2012-07-19 : 10:52:38
I have a derived table TIME_MASTER
that looks like this:
Fullname 7/14/2012 7/7/2012 6/29/2012
s1/sdhd1 62.12 79.14 78.33
s3/sdhd1 22.01 100
s5/sdhd1 27.66 90.98 92.46
s2/sdhd1 99.10 100 99.89

Is it possible to sort always 2nd dynamically created column in desc order by knowing that the date headers - date columns are generated always automatically? Sample sorted output of this would be:


Fullname 7/14/2012 7/7/2012 6/29/2012
s2/sdhd1 99.10 ... ...
s1/sdhd1 62.12 ...
s5/sdhd1 27.66 ... ...
s3/sdhd1 22.01 ... ...

thsi is how this table is derived:

declare @temp1 table
(
FullName varchar(455),
[1 year] varchar(20),
[6 months] varchar(20
)

select t.[1 year], t.[6 months], o.*
INTO
TIME_MASTER
from HIST_MASTER o
LEFT JOIN @temp1 t
ON o.Fullname = t.FullName

sunitabeck
Master Smack Fu Yak Hacker

5155 Posts

Posted - 2012-07-19 : 11:00:15
You can add ORDER BY 2 DESC. The 2 indicates to SQL Server to sort by the second column in the select list.
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2012-07-19 : 11:11:10
hmm...why is left joined table columns at first? also you're not even using order by. then how can you guarantee order of retrieved data?

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

nietzky
Yak Posting Veteran

75 Posts

Posted - 2012-07-19 : 11:47:43
Thank you, problem solved.
Go to Top of Page
   

- Advertisement -