sql-order-by

10g ordering varchar columns when containing numbers in front

旧时模样 提交于 2019-12-12 10:56:38
问题 I have an Oracle 10g DB and have a VARCHAR2 (2000 Character) column lets name it TEST which can contain numbers in front for example: test 1test 3test When I call "... order by TEST asc" or simply "... order by TEST" I get the results ordered like test 1test 3test But I would like to get the results ordered like this: 1test 3test test So the numbered inserts first, is there a method to achieve this? 回答1: What is your NLS_SORT set to? ( select sys_context('USERENV', 'NLS_SORT') from dual ). If

Sort timestamps (including future) by absolute distance from “now”

放肆的年华 提交于 2019-12-12 10:48:59
问题 With a date field I can do this: ORDER BY ABS(expiry - CURRENT_DATE) With a timestamp field I get the following error: function abs(interval) does not exist 回答1: Use now() or CURRENT_TIMESTAMP for the purpose. The reason for the different outcome of your queries is this: When you subtract two values of type date , the result is an integer and abs() is applicable. When you subtract two values of type timestamp (or just one is a timestamp ), the result is an interval , and abs() is not

Order by char column numerically

半世苍凉 提交于 2019-12-12 10:16:26
问题 How to Sort Character column numerically. I have a column of numbers stored as chars. When I do a ORDER BY for this column I get the following: 100D 131A 200 21B 30 31000A etc. There may be chance of having one Alphabet at the end. How can I order these chars numerically? Do I need to convert something or is there already an SQL command or function for this? 回答1: You could use something like: ORDER BY Cast(regexp_replace(yourcolumn, '[^0-9]', '', 'g') as integer) 来源: https://stackoverflow.com

Replacing order by rand mysql

强颜欢笑 提交于 2019-12-12 09:50:54
问题 Are there any equivalents of order by rand ? 回答1: While link from @Sandro explains why ORDER BY RAND() is bad, it gives no real solutions. You should try reading this articles. And if it is too "dry", or you want to learn more about SQL, then you should look up SQL Antipatters book, or at least carefully look through authors slides. 来源: https://stackoverflow.com/questions/5538651/replacing-order-by-rand-mysql

php Mysql Grouping and Ordering user messages together

空扰寡人 提交于 2019-12-12 09:49:32
问题 PHP Mysql Table:Messages id sender receiver time_sent Message SendDel RecDel 1 1 3 2011-08-17 14:00:00 [text] 0 0 2 3 1 2011-08-17 15:00:00 [text] 0 0 3 2 4 2011-08-18 14:19:28 [text] 1 0 4 4 2 2011-08-18 15:19:28 [text] 0 0 Objective is to retrieve the highest value message(MAX) and group the messages based on sender,receiver. So message id's 1 and 2 would group together and id's 3 and 4 would group together. Example: logged in userid = 2 Should return id sender receiver time_sent Message

Mongoid, how to order_by through a references_one association (and subsequent associations)?

大城市里の小女人 提交于 2019-12-12 09:45:04
问题 Simple model: class hat embedded_in :owner field :color end class owner embedds_one :hat referenced_in :house field :name end class house references_one :owner field :number end So simply puts, we have collection of houses that are associated to an owner, and the owner can have a colored hat. I can simply sort the house by their number: House.all.order_by( [[ :number, :asc ]]) But what I want is ordering the house, by the name of their owner, ideally I would like to write: House.all.order_by(

Sequence the Properties in a C# Class

感情迁移 提交于 2019-12-12 09:41:37
问题 We have a file format we need to parse that looks like: v1|000|sammy|endpoint|blah It's an ordered fixed-width format a vendor provides to us, so each of those 5 fields maps to a specific property in the class (the actual format has >30). I'd like to just parse this with Reflection by applying sequence to the properties. One way I could do this is to just make something up myself - write an Attribute class that takes a single number, and apply that attribute to each property with its sequence

Return records distinct on one column but order by another column

拈花ヽ惹草 提交于 2019-12-12 09:15:41
问题 I am building a Rails 3 app with a pretty standard message model. I would like to return the most recently created message records for each unique conversation_id. It seems like a fairly simple task, but I have not been able to code or find a working solution. Admittedly, I am not super SQL savvy either (as I have gotten by with mainly Active Record queries thus far). Here is what I'm trying to accomplish. Sample messages table: | id | sender_id | receiver_id | conversation_id | subject |

SQL Query: Need order by count, most must be on top, the rest follows

旧时模样 提交于 2019-12-12 08:09:09
问题 TABLEA JobCode Job1 Job2 Job3 zip ------- ---- ---- ---- ---------- F F S NULL 90030 F F S NULL 90031 F F S NULL 90031 F F S NULL 90034 F F NULL 90034 F F S NULL 90034 F F S NULL 90034 F F NULL 90034 F F S NULL 90035 F F NULL 90035-4640 EXPECTED RESULTS: JobCode Job1 Job2 Job3 zip ------- ---- ---- ---- ---------- F F S NULL 90034 F F NULL 90034 F F S NULL 90034 F F S NULL 90034 F F NULL 90034 F F S NULL 90031 F F S NULL 90031 F F S NULL 90030 F F S NULL 90035 F F NULL 90035-4640 Those with

ORDER BY with columns that are sometimes empty

跟風遠走 提交于 2019-12-12 07:59:59
问题 My SQL looks something like this: SELECT CompanyName , LastName , FirstName FROM ... JOIN ... ORDER BY CompanyName , LastName , FirstName Now the problem is that column A is sometimes empty (either as NULL or "" ), and I don't want all those results to turn up in the end. In the example, I'd like to have the fourth entry (which starts with a C) to be the third. But if I just ORDER BY, this happens: Avagax Bauer Frank Bele AG Smith John Mork AG Baggins Frodo Chen Jun In addition, I sometimes