min-max string (continue)

See related question:
http://www.experts-exchange.com/Microsoft/Development/MS-SQL-Server/SQL-Server-2005/Q_25942283.html

I would like to use Order by in the query, but I am not sure how to do that. Could you please help?
johnkainnAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

ThomasianCommented:
Which query did you use? What field do you need to sort the result?

If you just need to sort the result using one of the fields, you can just add ORDER BY at the end of the query.

e.g.

SELECT ..
FROM...
ORDER BY ANumText
0
johnkainnAuthor Commented:
Can you please have a look at the related question or click the link above.
I would like to use order by in the query in the last comment.
0
ThomasianCommented:
Ok. But you haven't answered my other question. On which column do you need to sort?

If you just need to result sorted using a column, you just need to add ORDER BY at the end of the select statement followed by the column name/number.

SELECT T.TId, T.Num, COALESCE(A.ANumText, CAST(T.TDefault as varchar)) ANumText
FROM @T T OUTER APPLY
    (SELECT CASE WHEN MIN(ANum)<>MAX(ANum)
                 THEN CAST(MIN(ANum) as varchar) + ' - ' + CAST(MAX(ANum) as varchar)
                 WHEN MIN(ANum)<>0
                 THEN CAST(MIN(ANum) as varchar)
            END ANumText
       FROM @A
       WHERE TId = T.TId
    ) A
ORDER BY ANumText
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Protecting & Securing Your Critical Data

Considering 93 percent of companies file for bankruptcy within 12 months of a disaster that blocked access to their data for 10 days or more, planning for the worst is just smart business. Learn how Acronis Backup integrates security at every stage

sevlaganCommented:

SELECT a.TID, CASE WHEN MAX(b.ANum) = MIN(b.ANum) THEN CAST( MIN(b.ANum) as VARCHAR(5) )  
                  ELSE CAST( MIN(b.ANum) AS VARCHAR(5)) + '-' + CAST( MAX(b.ANum) as VARCHAR(5) )      END AS ANumText
FROM @T AS a JOIN @A AS b ON a.TId = b.TId
GROUP BY a.TId
0
ronan1979Commented:
even if you don't need anymore, someone else might - about the subject - Trees in SQL SERVER : http://msdn.microsoft.com/en-us/library/aa175807%28SQL.80%29.aspx

as said @Thomasian

ASC ascendant order
DESC descendant order

ORDER BY T.TId ASC;
ORDER BY T.TId DESC;
ORDER BY T.Num DESC;
ORDER BY T.Num DESC;
ORDER BY T.ANumText DESC;
ORDER BY T.ANumText DESC;
0
johnkainnAuthor Commented:
I have an additional column in @T table that is called MyText.
I need to sort by it. If I add Order by MyText at the end of the query I get returned multiple rows for each row in @T.

0
ThomasianCommented:
Can you post the actual query? Adding an order by clause on the query should not cause it to return more/less records.
0
sevlaganCommented:
This should work:

SELECT a.TID, CASE WHEN MAX(b.ANum) = MIN(b.ANum) THEN CAST( MIN(b.ANum) as VARCHAR(5) )  
                  ELSE CAST( MIN(b.ANum) AS VARCHAR(5)) + '-' + CAST( MAX(b.ANum) as VARCHAR(5) )      END AS ANumText
FROM @T AS a JOIN @A AS b ON a.TId = b.TId
GROUP BY a.TId
0
johnkainnAuthor Commented:
Thanks. I found out the problem.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server 2005

From novice to tech pro — start learning today.