Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


[SQL Server 2005] Problem with ORDER BY clause


[SQL Server 2005] Problem with ORDER BY clause

Author
Message
Eugene Elutin
Eugene Elutin
Hall of Fame
Hall of Fame (3K reputation)Hall of Fame (3K reputation)Hall of Fame (3K reputation)Hall of Fame (3K reputation)Hall of Fame (3K reputation)Hall of Fame (3K reputation)Hall of Fame (3K reputation)Hall of Fame (3K reputation)

Group: General Forum Members
Points: 3042 Visits: 5478
cms9651 (9/6/2012)
The rule for the sorting is mat geographic NAME.

MAW = My Area West
MAE = My Area East
MAC = My Area Center
MAS = My Area South


Oh, why we couldn't see that from your first post. It is so obvious ... (yes it's a sarcasm) Hehe

If you understand how I've made 'Tot' to appear as a last one, you should be able to figure out how to force the order based on the above requirement. You will need to hard-code priority, or create and use table which list your Geographic NAMEs where you can maintain the order sequence.

1. Just hard-coded order priority



SELECT
[MAT],
[DVD],
[SALES]
FROM
TestTable
ORDER BY DVD DESC, CASE MAT WHEN 'MAW' THEN 1
WHEN 'MAE' THEN 2
WHEN 'MAC' THEN 3
WHEN 'MAS' THEN 4
ELSE 5 --'Tot'
END




2. With order sequence maintained in dedicated table


CREATE TABLE dbo.RefGeographic (Code CHAR(3), Description VARCHAR(50), OrderSequence INT)
INSERT dbo.RefGeographic SELECT 'MAW','My Area West',1
INSERT dbo.RefGeographic SELECT 'MAE','My Area East',2
INSERT dbo.RefGeographic SELECT 'MAC','My Area Center',3
INSERT dbo.RefGeographic SELECT 'MAS','My Area South',4
INSERT dbo.RefGeographic SELECT 'MAN','My Area North',5

-- now you can use it in your query
SELECT t.[MAT],
t.[DVD],
t.[SALES]
FROM TestTable t
LEFT JOIN RefGeographic g
ON g.Code = t.MAT
ORDER BY DVD DESC, ISNULL(g.OrderSequence, 9999999)



You can see that a second way will allow you to change the order (if it will be ever required) without code change. Also it will work great in case if new geographic names will need to be added (eg. "My Area South-West" and "My Area Middle Of Nowhere")Hehe

_____________________________________________
"The only true wisdom is in knowing you know nothing"
"O skol'ko nam otkrytiy chudnyh prevnosit microsofta duh!":-D
(So many miracle inventions provided by MS to us...)

How to post your question to get the best and quick help
Sean Lange
Sean Lange
SSCoach
SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)

Group: General Forum Members
Points: 16550 Visits: 17004
cms9651 (9/6/2012)
The rule for the sorting is mat geographic NAME.

MAW = My Area West
MAE = My Area East
MAC = My Area Center
MAS = My Area South


Ahh there is the challenge you were running into. You didn't have anything to order by.

Try this.


ORDER BY [DVD] DESC, CASE WHEN MAT = 'Tot' THEN 1 ELSE 0 END, CASE MAT when 'MAW' then 0 when 'MAE' then 1 when 'MAC' then 2 when 'MAS' then 4 end



yep as I was typing Eugene posted pretty much the same thing. :-D

_______________________________________________________________

Need help? Help us help you.

Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

Need to split a string? Try Jeff Moden's splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
cms9651
cms9651
Valued Member
Valued Member (64 reputation)Valued Member (64 reputation)Valued Member (64 reputation)Valued Member (64 reputation)Valued Member (64 reputation)Valued Member (64 reputation)Valued Member (64 reputation)Valued Member (64 reputation)

Group: General Forum Members
Points: 64 Visits: 144
thanks a lot for help!
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search