SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Order by clause


Order by clause

Author
Message
sanjay.kumar-853392
sanjay.kumar-853392
Grasshopper
Grasshopper (22 reputation)Grasshopper (22 reputation)Grasshopper (22 reputation)Grasshopper (22 reputation)Grasshopper (22 reputation)Grasshopper (22 reputation)Grasshopper (22 reputation)Grasshopper (22 reputation)

Group: General Forum Members
Points: 22 Visits: 83
want to order in a particular column having thr below data.

1
a1
d1
12
0
90
9800
.

i need the output 1 mast be the first alue than the rest values in sorted order
ChrisM@Work
ChrisM@Work
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: 16232 Visits: 19549
ORDER BY RIGHT('00000'+MyColumn, n)
where n is the length of you column. Ensure that there are enough 0's.

“Write the query the simplest way. If through testing it becomes clear that the performance is inadequate, consider alternative query forms.” - Gail Shaw

For fast, accurate and documented assistance in answering your questions, please read this article.
Understanding and using APPLY, (I) and (II) Paul White
Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden
Exploring Recursive CTEs by Example Dwain Camps
RBarryYoung
RBarryYoung
SSChampion
SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)

Group: General Forum Members
Points: 14730 Visits: 9518
Something like this:

Select *
From yourTable
Order By (Case When col=1 then 0 else 1 End), Col1



-- RBarryYoung, (302)375-0451 blog: MovingSQL.com, Twitter: @RBarryYoung
Proactive Performance Solutions, Inc.
"Performance is our middle name."
RBarryYoung
RBarryYoung
SSChampion
SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)

Group: General Forum Members
Points: 14730 Visits: 9518
Hmm, I think that we must have interpreted his post differently, Chris.

-- RBarryYoung, (302)375-0451 blog: MovingSQL.com, Twitter: @RBarryYoung
Proactive Performance Solutions, Inc.
"Performance is our middle name."
ChrisM@Work
ChrisM@Work
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: 16232 Visits: 19549
rbarryyoung (12/14/2008)
Hmm, I think that we must have interpreted his post differently, Chris.

Heh - yeah, think so. I didn't read it properly first time. "1" first, followed by everything else including "0", in an unspecified sorted order.

Sanjay, can you list these items in the order in which you wish them to appear?

“Write the query the simplest way. If through testing it becomes clear that the performance is inadequate, consider alternative query forms.” - Gail Shaw

For fast, accurate and documented assistance in answering your questions, please read this article.
Understanding and using APPLY, (I) and (II) Paul White
Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden
Exploring Recursive CTEs by Example Dwain Camps
sanjay.kumar-853392
sanjay.kumar-853392
Grasshopper
Grasshopper (22 reputation)Grasshopper (22 reputation)Grasshopper (22 reputation)Grasshopper (22 reputation)Grasshopper (22 reputation)Grasshopper (22 reputation)Grasshopper (22 reputation)Grasshopper (22 reputation)

Group: General Forum Members
Points: 22 Visits: 83
Hi! Chris Morris
Thanx for reply




region Id ParentId Text
a1 1 wwer
b1 1 yrt
c1 1 ryrt
a11 a1 eryrty
b11 b1 nbnvnfth
c11 c1 vc
b111 b11 vbc
a111 a11 cb
c111 c11 bc



result should be

a1 1 wwer
b1 1 yrt
c1 1 ryrt
then all rest values sort order by parent id


The major issues is this that the parent id is generated by Java Random method as string and it can be any thing
but the first parent id is assign the manual

i hope you got the issues ,

waiting for ur reply

Regards,
Sanjay
ChrisM@Work
ChrisM@Work
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: 16232 Visits: 19549
Hi Sanjay

I'm still not totally sure of your requirement but try this - it will generate the sort order you're expecting from the given sample data.

DROP TABLE #Temp
CREATE TABLE #Temp (regionId VARCHAR(10), ParentId VARCHAR(10), TextColumn VARCHAR(10))
INSERT INTO #Temp (regionId, ParentId, TextColumn)
SELECT 'a1', '1', 'wwer' UNION ALL
SELECT 'b1', '1', 'yrt' UNION ALL
SELECT 'c1', '1', 'ryrt' UNION ALL
SELECT 'a11', 'a1', 'eryrty' UNION ALL
SELECT 'b11', 'b1', ' nbnvnfth' UNION ALL
SELECT 'c11', 'c1', 'vc' UNION ALL
SELECT 'b111', 'b11', 'vbc' UNION ALL
SELECT 'a111', 'a11', 'cb' UNION ALL
SELECT 'c111', 'c11', 'bc'

SELECT RIGHT('000000000' + regionId, 10), *
FROM #Temp
ORDER BY RIGHT('000000000' + regionId, 10), ParentId



“Write the query the simplest way. If through testing it becomes clear that the performance is inadequate, consider alternative query forms.” - Gail Shaw

For fast, accurate and documented assistance in answering your questions, please read this article.
Understanding and using APPLY, (I) and (II) Paul White
Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden
Exploring Recursive CTEs by Example Dwain Camps
Garadin
Garadin
SSCrazy
SSCrazy (2.7K reputation)SSCrazy (2.7K reputation)SSCrazy (2.7K reputation)SSCrazy (2.7K reputation)SSCrazy (2.7K reputation)SSCrazy (2.7K reputation)SSCrazy (2.7K reputation)SSCrazy (2.7K reputation)

Group: General Forum Members
Points: 2721 Visits: 4107
This is Barry's original case method applied to your table structure.

SELECT * 
FROM #Temp
ORDER BY (CASE WHEN ParentID = '1' THEN 0 ELSE 1 END), ParentId



Notice the way Chris posted your table structure and sample data. Please use this format in future posts, as it makes things much easier for us to deal with. For more instructions on how to do this, please see the link in either of our signatures. Thanks for the inserts Chris. Cool

Seth Phelabaum
Consistency is only a virtue if you're not a screwup. ;-)

Links: How to Post Sample Data :: Running Totals :: Tally Table :: Cross Tabs/Pivots :: String Concatenation
ChrisM@Work
ChrisM@Work
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: 16232 Visits: 19549
'Welcome Seth Smile

“Write the query the simplest way. If through testing it becomes clear that the performance is inadequate, consider alternative query forms.” - Gail Shaw

For fast, accurate and documented assistance in answering your questions, please read this article.
Understanding and using APPLY, (I) and (II) Paul White
Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden
Exploring Recursive CTEs by Example Dwain Camps
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