Recent PostsRecent Posts Popular TopicsPopular Topics
 Home Search Members Calendar Who's On

 ORDER BY non-sequential number sequence Rate Topic Display Mode Topic Options
Author
 Message
 Posted Saturday, January 12, 2013 4:34 PM
 Forum Newbie Group: General Forum Members Last Login: Tuesday, January 15, 2013 10:15 AM Points: 5, Visits: 7
 I have a table where a certain column contains a status id as an integer. I want to sort on this field, but I want to sort in a specific order. Ie, instead of 1,2,3,4,5 I want the order returning as 3,2,4,5,1. That is, all records with statusid 3 first, then 2, etc. The only way I can think of is something like this:SELECT ... FROM ... WHERE ... ORDER BY CASE WHEN statusId = 3 THEN 0 WHEN stautsId = 2 THEN 1 ... ETC ...But I'm wondering if there's another way thats more readable that I've overlooked. I really don't want to do multiple DB hits unless its going to be quicker (queries done via php).
Post #1406421
 Posted Saturday, January 12, 2013 4:38 PM
 SSCommitted Group: General Forum Members Last Login: Today @ 12:09 AM Points: 1,651, Visits: 5,193
 Another way...ORDER BY CHARINDEX(statusId,'32451')
Post #1406422
 Posted Saturday, January 12, 2013 5:04 PM
 Forum Newbie Group: General Forum Members Last Login: Tuesday, January 15, 2013 10:15 AM Points: 5, Visits: 7
 Thanks, that looks great. However, the number can become 2 decimal places, so I thought something like:select ('S'+CAST ([statusId] AS VARCHAR(255))) as sid from tbl_xenixorder by CHARINDEX(sid,'S5S4')but it says 'sid' is an invalid column, despite the fact its in the column header if I miss out the 'order by' !Any clues?
Post #1406429
 Posted Sunday, January 13, 2013 1:39 AM
 Ten Centuries Group: General Forum Members Last Login: Today @ 12:44 AM Points: 1,058, Visits: 5,733
 select x.NewSid, sid from tbl_xenixCROSS APPLY (SELECT NewSid = ('S'+CAST ([statusId] AS VARCHAR(255)))) xorder by CHARINDEX(x.NewSid,'S5S4') Low-hanging fruit picker and defender of the moggies For better assistance in answering your questions, please read this. 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
Post #1406457
 Posted Sunday, January 13, 2013 2:46 AM
 SSC-Enthusiastic Group: General Forum Members Last Login: Thursday, April 10, 2014 5:49 AM Points: 187, Visits: 699
 You should do it another way. When normalizing the database there should be a table containg all available statusids. Your data table should then have a foreignn key to the statusid-table. When doing so you can add a column to the statusid-table where you define the sort order.Something like this:`create table dbo.domStatus( StatusId int not null, SortOrder int not null primary key ( StatusId ) );create table dbo.SomeDataTable( Col1 varchar(50), Col2 varchar(50), StatusId int not null );alter table dbo.SomeDataTable add constraint fk_SomeDataTable_domStatusforeign key ( StatusId ) references dbo.domStatus ( StatusId );select ...from dbo.SomeDataTable d inner join dbo.domStatus s on s.StatusId = d.StatusIdORDER BY s.SortOrder`
Post #1406459
 Posted Sunday, January 13, 2013 11:06 AM
 Forum Newbie Group: General Forum Members Last Login: Tuesday, January 15, 2013 10:15 AM Points: 5, Visits: 7
 Thanks, I actually do have such a table (it translates status id to name and description), so its pretty easy to just add another column with the sort order. Thanks for that, it completely slipped my mind to do it like that. Actually, I have several web pages, and currently the list of ids for each page is in the PHP file. I think I'll move it into the DB, it'll make it easier to change.I wish there was a way of thanking/voting users on this forum.
Post #1406497
 Posted Sunday, January 13, 2013 12:04 PM
 SSC-Insane Group: General Forum Members Last Login: Yesterday @ 9:47 PM Points: 22,472, Visits: 30,138
 steve 50602 (1/13/2013)Thanks, I actually do have such a table (it translates status id to name and description), so its pretty easy to just add another column with the sort order. Thanks for that, it completely slipped my mind to do it like that. Actually, I have several web pages, and currently the list of ids for each page is in the PHP file. I think I'll move it into the DB, it'll make it easier to change.I wish there was a way of thanking/voting users on this forum.I wish there was a way of thanking/voting users on this forum. << You just did.
Post #1406499
 Posted Sunday, January 13, 2013 3:39 PM
 SSCommitted Group: General Forum Members Last Login: Today @ 12:09 AM Points: 1,651, Visits: 5,193
 WolfgangE (1/13/2013)You should do it another way. When normalizing the database there should be a table containg all available statusids. Your data table should then have a foreignn key to the statusid-table. When doing so you can add a column to the statusid-table where you define the sort order.Something like this:`create table dbo.domStatus( StatusId int not null, SortOrder int not null primary key ( StatusId ) );create table dbo.SomeDataTable( Col1 varchar(50), Col2 varchar(50), StatusId int not null );alter table dbo.SomeDataTable add constraint fk_SomeDataTable_domStatusforeign key ( StatusId ) references dbo.domStatus ( StatusId );select ...from dbo.SomeDataTable d inner join dbo.domStatus s on s.StatusId = d.StatusIdORDER BY s.SortOrder`While that may be a good idea for any incredibly simple system, what do you do when you want to order things differently? Add more columns to the domStatus table? No thanks.What about when you want the end user to choose a sort order? Add all possible combinations to your domStatus table? No thanks.There are times when IMHO you can take "Normalisation" too far and this may be one of them, don't you think? Unless you can definitely say that anything with a status will always need to be ORDERED exactly the same way, I would steer clear of putting that sort of metadata into the database design.If you use something simple like the CHARINDEX example, you can provide flexibility at very little cost to design. But this is just my opinion and I do appreciate the alternative view put forward by Wolfgang
Post #1406521
 Posted Sunday, January 13, 2013 4:08 PM
 Forum Newbie Group: General Forum Members Last Login: Tuesday, January 15, 2013 10:15 AM Points: 5, Visits: 7
 I completely agree with you, but in this case, the sort order will not be changeable by the user (ever), and there will only ever be one sort order. If the order is ever requested to be changed, then its a simple method of updating a table rather than digging around in the PHP code. If I wanted the sort order changeable via the website, then I would definitely use something like CHARINDEX, as it seems a pretty flexible solution. I'd probably just put the concatenation and CAST inside the CHARINDEX function, rather than using a CROSS APPLY.
Post #1406525
 Posted Monday, January 14, 2013 12:43 AM
 SSC-Enthusiastic Group: General Forum Members Last Login: Thursday, April 10, 2014 5:49 AM Points: 187, Visits: 699
 I cannot agree to that and in my opinion there is just one correct answer to this: it depends!What do you call an "incredibly simple system"? How do you call other systems? And how do know all the background about the one requirment of some special sorting?The way I have shown works perfekt for some requirements, e.g. sorting the items for cash desk accountings in meaningful way that is standard for all 400 shops.On the other hand I have never seen a website where a user can define how single values have to be sorted. I only have seen the possibility of giving a column name for sorting, ascending or descending. The disadvantages using the cross-apply-method I see are performance and maintenance:If the number of rows of the outer select increases the query will slow down as the apply-operator does the subselect for any outer row.And what do you do when you have to change the sort logic or if there is a new statusid? In how many procedures will you have to change your code? Will you even find them all? A simple update of 1 table with a column for sort order seems to be much better.But as I said: it depends. Giving the user the possibility to choose a column for sorting or even let him decide, which values within a column are sorted as the user wants might be difficult. I wonder if this would even be part of the database procedure at all. On huge systems there might be thougts like "how many execution plan for one query do I want in my procedure cache?" or "will my execution plans be cached and reused?" Maybe it's more efficient doing the query without sorting on database level and sort the data in the frontend-programming.Yes, there are many aspects. Depending on the background of the requirement you will have to find an appropriate solution.
Post #1406580

 Permissions