sql-order-by

How to sort by numbers first with Oracle SQL query?

旧街凉风 提交于 2019-11-27 18:27:11
问题 I have this table with a 'title' field which is varchar2 and I want to select all rows and sort them first by number and then by the alphabet as it normally happens. For instance, I currently get this using a simple ORDER BY title in the end: Abc Def 321 But I want this: 321 Abc Def The weird thing is that SQL Developer shows the "right" order, with numbers first. But on my app (PHP using OCI8) it shows numbers last. 回答1: Not an Oracle expert, but you are supposed to be able to do it without

Can I get the position of a record in a SQL result table?

女生的网名这么多〃 提交于 2019-11-27 18:24:07
问题 If I do something like SELECT * FROM mytable ORDER BY mycolumn ASC; I get a result table in a specific order. Is there a way in SQL to efficiently find out, given a PK, what position in that result table would contain the record with my PK? 回答1: You can count the number of records where the value that you are sorting on has a lower value than the record that you know the key value of: select count(*) from mytable where mycolumn < (select mycolumn from mytable where key = 42) 回答2: On databases

MySQL Sort By 2 Columns

扶醉桌前 提交于 2019-11-27 17:49:35
问题 I have a table which holds information on television programs and I want to order it by Seasons and then by episodes. Here's a basic view of what I have: +---+--------+---------+ |id | Season | Episode | +---+--------+---------+ | 1 | 1 | 1 | +---+--------+---------+ | 1 | 1 | 2 | +---+--------+---------+ | 1 | 2 | 1 | +---+--------+---------+ | 1 | 2 | 3 | +---+--------+---------+ So I select what I need and order by Season. But there's going to be a lot between seasons so I need to sort

Custom ORDER BY Explanation

不问归期 提交于 2019-11-27 17:48:46
I found this some time ago and have been using it since; however, looking at it today, I realized that I do not fully understand why it works. Can someone shed some light on it for me? ORDER BY s.type!= 'Nails', s.type!= 'Bolts', s.type!= 'Washers', s.type!= 'Screws', s.type!= 'Staples', s.type!= 'Nuts', ... If I order by s.type, it orders alphabetically. If I use the example above it uses the same order as the line positions. What I don't understand is the use of !=. If I use = it appears in the opposite order. I cannot wrap my head around the concept of this. It would reason to me that using

Order by multiple columns with Doctrine

六眼飞鱼酱① 提交于 2019-11-27 17:33:41
I need to order data by two columns (when the rows have different values for column number 1, order by it; otherwise, order by column number 2) I'm using a QueryBuilder to create the query. If I call the orderBy method a second time, it replaces any previously specified orderings. I can pass two columns as the first parameter: ->orderBy('r.firstColumn, r.secondColumn', 'DESC'); But I cannot pass two ordering directions for the second parameter, so when I execute this query the first column is ordered in an ascending direction and the second one, descending. I would like to use descending for

Django custom order_by

谁都会走 提交于 2019-11-27 16:53:47
问题 I've got a model eg. Car with a foreign key eg. Owner, which may or may not be blank. The Car has a creation_date. I would like to order these cars by date, but if the car has an owner, the date of birth of the owner must be taken instead of the creation_date of the car. Is this possible? 回答1: Have a look at this similar question: Good ways to sort a queryset? - Django You can't use the model's Meta ordering as it only accepts one field https://docs.djangoproject.com/en/dev/ref/models/options

Oracle SQL returns rows in arbitrary fashion when no “order by” clause is used

自作多情 提交于 2019-11-27 16:31:34
问题 Maybe someone can explain this to me, but when querying a data table from Oracle, where multiple records exist for a key (say a customer ID), the record that appears first for that customer can vary if there is no implicit "order by" statement enforcing the order by say an alternate field such as a transaction type. So running the same query on the same table could yield a different record ordering than from 10 minutes ago. E.g., one run could yield: Cust_ID, Transaction_Type 123 A 123 B

What does LISTAGG with ORDER BY NULL actually use as the order criteria?

耗尽温柔 提交于 2019-11-27 14:50:54
问题 If I do SELECT LISTAGG( COLUMN_VALUE ) WITHIN GROUP ( ORDER BY NULL ) AS OrderByNULL, LISTAGG( COLUMN_VALUE ) WITHIN GROUP ( ORDER BY 1 ) AS OrderByCONST, LISTAGG( COLUMN_VALUE ) WITHIN GROUP ( ORDER BY ROWNUM ) AS OrderByROWNUM FROM TABLE( SYS.ODCIVARCHAR2LIST( '5', '222', '4' ) ); The output is: ORDERBYNULL ORDERBYCONST ORDERBYROWNUM ----------- ------------ ------------- 222,4,5 222,4,5 5,222,4 The query appears to have done an alphanumerical sort when using ORDER BY with non-deterministic

ActiveRecord Find All not sorting by ID?

这一生的挚爱 提交于 2019-11-27 14:39:27
I've got a strange issue on a Heroku deployment that I can't seem to duplicate locally. Basically when I find all on a specific model instead of sorting by ID it seems to return them in no order at all. Typically the records come out like so: >> Model.all => [<model id: 2>,<model id: 1>,<model id: 3>,<model id: 4>,<model id: 5>] ... and so on. If I explicitly call Model.order("id ASC") it returns the models as expected. What gives? Why would find all not return the objects in descending ID order? Ordering by ID is not guaranteed by default. It’s up to the database how a non-ordered query gets

How can I order entries in a UNION without ORDER BY?

情到浓时终转凉″ 提交于 2019-11-27 14:34:31
问题 How can I be sure that my result set will have a first and b second? It would help me to solve a tricky ordering problem. Here is a simplified example of what I'm doing: SELECT a FROM A LIMIT 1 UNION SELECT b FROM B LIMIT 1; 回答1: SELECT col FROM ( SELECT a col, 0 ordinal FROM A LIMIT 1 UNION ALL SELECT b, 1 FROM B LIMIT 1 ) t ORDER BY ordinal 回答2: I don't think order is guaranteed, at least not across all DBMS. What I've done in the past to control the ordering in UNIONs is: (SELECT a, 0 AS