sql-order-by

how to use order by in alphanumeric column in oracle

只愿长相守 提交于 2019-12-23 12:26:15
问题 In my table one of column i have a value like below Y-1 Y-2 Y-3 Y-4 Y-5 Y-6 Y-7 Y-8 Y-9 Y-10 Y-11 Y-12 Y-13 Y-14 when i am order by this column its working fine if the row has value up to Y-9 other wise my result is wrong like below. Y-1 Y-10 Y-11 Y-12 Y-13 Y-14 Y-2 Y-3 Y-4 Y-5 Y-6 Y-7 Y-8 Y-9 But i want the output like below Y-1 Y-2 Y-3 Y-4 Y-5 Y-6 Y-7 Y-8 Y-9 Y-10 Y-11 Y-12 Y-13 Y-14 How to acheive the above result.i am using oracle database.Any help will be greatly appreciated!!!!! 回答1:

Django Order_BY custom function

隐身守侯 提交于 2019-12-23 11:59:10
问题 I have field in model roll_numb . The roll_numb has values as follows. 070-001 070-007 070-343 080-002 080-008 When i order_by roll_numb the sorting is as it is above. I want to split the roll_numb by - and sort by the remainder. (i.e. 001, 002, 008) Code class Meta: ordering = ['roll_numb'] 回答1: I think it is not possible to order a queryset by method of model in Django's ORM scope. So in order to sort your queryset by your custom method, I recommend 2 ways: First qs = mymodel.objects.all()

SQL Custom Order By

感情迁移 提交于 2019-12-23 10:05:52
问题 I can't understand why this doesn't work: select distinct a.QuestionID,a.QuestionName,b.AnswerID,b.AnswerName from @TempExportList a join tblAnswers b on a.QuestionID = b.QuestionID where a.PaperID=@PaperID order by (case when a.QuestionName='A' then 0 when a.QuestionName='B' then 1 else a.QuestionID end) I get the following error - ORDER BY items must appear in the select list if SELECT DISTINCT is specified. But this works fine: select distinct a.QuestionID,a.QuestionName,b.AnswerID,b

Mysql ORDER BY using date data row

空扰寡人 提交于 2019-12-23 08:05:16
问题 I have a query something like this: SELECT title, desc, date FROM tablename ORDER BY date ASC, title ASC; Works fine when the data actually has a date. Issue is, date submission is optional, so I sometimes get 0000-00-00 as a date, which has the unfortunate effect of placing all nondated rows on top. So, I then tried this: SELECT title, desc, date FROM tablename ORDER BY date DESC, title ASC; Which sort of works, but not really -- all items with dates (non 0000-00-00) get listed in descending

SQL Server 2005: Order with NULL values at the end [duplicate]

寵の児 提交于 2019-12-23 07:20:06
问题 This question already has answers here : Closed 7 years ago . Possible Duplicate: Case Order by using Null I'm looking to get a list of records ordered by an "ordernum" field. The ordernum field is an int field. This field starts as NULL until set by a user. I would like the NULL entries to appear at the end of the list. I am building a query as follows: select *, case when (ordernum is null) then [largestInt] else ordernum end as newordernum from tableName order by newordernum I know I could

MySQL Sorting by turns

别等时光非礼了梦想. 提交于 2019-12-23 06:29:27
问题 I want to sort the following data items in the order they are presented below : id | res -------- 1 A 2 A 3 A 4 A 5 A 6 B 7 B 8 B 9 B 10 B 11 C 12 C 13 C 14 C 15 C And i want the output is : res --- A B C A B C A B C A B C A B C Any tricks to make it sort more properly? Many Thanks! 回答1: You can order by the "rank" within the group. The rank can be calculated using a self join and counting the entries with lesser ids. select t1.id, t1.res from mytable t1 join mytable t2 on t2.res = t1.res and

MySQL Sorting by turns

大憨熊 提交于 2019-12-23 06:29:04
问题 I want to sort the following data items in the order they are presented below : id | res -------- 1 A 2 A 3 A 4 A 5 A 6 B 7 B 8 B 9 B 10 B 11 C 12 C 13 C 14 C 15 C And i want the output is : res --- A B C A B C A B C A B C A B C Any tricks to make it sort more properly? Many Thanks! 回答1: You can order by the "rank" within the group. The rank can be calculated using a self join and counting the entries with lesser ids. select t1.id, t1.res from mytable t1 join mytable t2 on t2.res = t1.res and

Sorting table by different cols, depending on what happens to another column

谁说我不能喝 提交于 2019-12-23 04:28:10
问题 This is my first time I'm asking a question, and English is not my native language. I apologize for any misspelling or misbehaving beforehand. Now to my question: I have a table looking like this. (image 1) 1. Every booked time is half an hour long 2. There are always booking_date, hour and minute 3. Some rows have got delivery_date, some hasn’t yet 4. Delivery_date are always AFTER booking_date unordered table If the day being printed out is for example (2018-12-01), I want the table to be

postgresql, odd OFFSET/LIMIT behavior ( records order )

橙三吉。 提交于 2019-12-23 02:24:06
问题 so basically I have this scope(sql): scope.to_sql => "SELECT \"offers\".* FROM \"offers\" INNER JOIN \"cities_offers\" ON \"offers\".\"id\" = \"cities_offers\".\"offer_id\" WHERE \"cities_offers\".\"city_id\" = 2 AND \"offers\".\"category_id\" IN (2) AND (offers.category_id is NOT NULL) ORDER BY offers.discount desc LIMIT 25 OFFSET 0" Somehow the records order is different for the above query and the same one without LIMIT and OFFSET: scope[6] => #<Offer id: 8629 ... scope.except(:offset,

thinking sphinx ordering by mixing capitals and lower case

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-23 02:06:47
问题 I have a rails application that is using thinking_sphinx for the searching. My problem is that the result returned is sorted with capitals first and lower case after it at the bottom. I'd like to mix them so that both 'A' and 'a' comes before 'B'. This is the method I'm using: Company.search(query, :star => true, :page => params[:page], :per_page => 20, :order => :name, :sort_mode => :asc) 回答1: Maurício's answer is almost on the right track - but that's for transforming the text that Sphinx