Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

Order by clause Expand / Collapse
Author
Message
Posted Sunday, December 14, 2008 12:53 PM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Saturday, November 24, 2012 12:58 AM
Points: 22, Visits: 82
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



Post #619336
Posted Sunday, December 14, 2008 1:42 PM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Today @ 3:19 AM
Points: 6,721, Visits: 13,832
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
Post #619341
Posted Sunday, December 14, 2008 1:45 PM


SSCrazy Eights

SSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy Eights

Group: General Forum Members
Last Login: Thursday, June 5, 2014 10:54 AM
Points: 9,294, Visits: 9,480
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."
Post #619342
Posted Sunday, December 14, 2008 1:47 PM


SSCrazy Eights

SSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy Eights

Group: General Forum Members
Last Login: Thursday, June 5, 2014 10:54 AM
Points: 9,294, Visits: 9,480
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."
Post #619343
Posted Monday, December 15, 2008 2:17 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Today @ 3:19 AM
Points: 6,721, Visits: 13,832
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
Post #619434
Posted Monday, December 15, 2008 2:28 AM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Saturday, November 24, 2012 12:58 AM
Points: 22, Visits: 82
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
Post #619437
Posted Monday, December 15, 2008 2:57 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Today @ 3:19 AM
Points: 6,721, Visits: 13,832
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
Post #619444
Posted Monday, December 15, 2008 6:53 AM


SSCommitted

SSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommitted

Group: General Forum Members
Last Login: Tuesday, April 22, 2014 9:38 AM
Points: 1,519, Visits: 4,071
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.


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
Post #619551
Posted Monday, December 15, 2008 6:56 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Today @ 3:19 AM
Points: 6,721, Visits: 13,832
'Welcome Seth :)

“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
Post #619554
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse