sql-order-by

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

泪湿孤枕 提交于 2019-11-29 02:13:04
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 Unless an "order by Transaction_Type" clause is used, Oracle could arbitrarily return the following result

Incorrect usage of UNION and ORDER BY?

北慕城南 提交于 2019-11-29 01:27:52
问题 how can i use union and order by in mysql ? select * from _member_facebook inner join _member_pts ON _member_facebook._fb_owner=_member_pts._username where _member_facebook._promote_point = 9 ORDER BY RAND() limit 2 UNION ALL select * from _member_facebook inner join _member_pts ON _member_facebook._fb_owner=_member_pts._username where _member_facebook._promote_point = 8 limit 3 give me error #1221 - Incorrect usage of UNION and ORDER BY any one can help ? 回答1: Try with: ( select * from

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

僤鯓⒐⒋嵵緔 提交于 2019-11-28 23:31:00
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 ordering ( NULL or a constant) and has maintained the input order when using ORDER BY ROWNUM

Orderby() not ordering numbers correctly c#

会有一股神秘感。 提交于 2019-11-28 22:28:24
I am writing an app for my company and am currently working on the search functionality. When a user searches for an item, I want to display the highest version (which is stored in a database). The problem is, the version is stored as a string instead of int, and when I do an OrderBy(q=>q.Version) on the results, they are returned like 1 10 11 2 3 ... Obviously 2 comes before 10. Is there a way for me to cast the version as an integer or is there a simple IComparer out there? I couldn't find anything substantial thus far. I tried doing this: var items = (from r in results select r).OrderBy(q =

Custom order by in SQL server like P, A, L, H [closed]

无人久伴 提交于 2019-11-28 22:19:53
问题 Closed . This question needs details or clarity. It is not currently accepting answers. Want to improve this question? Add details and clarify the problem by editing this post. Closed 6 years ago . Not ASC or DESC .... Order by custom... I have tried using case but not successfully SELECT * FROM Customers ORDER BY case country when 'P' then 1 … This is what I want: 回答1: SELECT * FROM Customers ORDER BY case when country = 'P' then 1 when country = 'A' then 2 when country = 'L' then 3 when

mysql order by with union doesn't seem to work

柔情痞子 提交于 2019-11-28 21:25:30
Here is my query (SELECT * FROM `jokes` WHERE `flags` < 5 AND (`title` LIKE "%only three doors%" OR `joke` LIKE "%only three doors%") ORDER BY `ups` DESC,`downs` ASC) UNION (SELECT * FROM `jokes` WHERE `flags` < 5 AND (`title` LIKE "%only%" OR `joke` LIKE "%only%") ORDER BY `ups` DESC,`downs` ASC) UNION (SELECT * FROM `jokes` WHERE `flags` < 5 AND (`title` LIKE "%three%" OR `joke` LIKE "%three%") ORDER BY `ups` DESC,`downs` ASC) UNION (SELECT * FROM `jokes` WHERE `flags` < 5 AND (`title` LIKE "%doors%" OR `joke` LIKE "%doors%") ORDER BY `ups` DESC,`downs` ASC) LIMIT 0, 30 For some reason it

Order by day_of_week in MySQL

情到浓时终转凉″ 提交于 2019-11-28 20:46:55
How can I order the mysql result by varchar column that contains day of week name? Note that MONDAY should goes first, not SUNDAY. Glen Solsberry Either redesign the column as suggested by Williham Totland, or do some string parsing to get a date representation. If the column only contains the day of week, then you could do this: ORDER BY FIELD(<fieldname>, 'MONDAY', 'TUESDAY', 'WEDNESDAY', 'THURSDAY', 'FRIDAY', 'SATURDAY', 'SUNDAY'); Joseph Manganelli Why not this? ORDER BY ( CASE DAYOFWEEK(dateField) WHEN 1 THEN 7 ELSE DAYOFWEEK(dateField) END ) I believe this orders Monday to Sunday... I'm

How do you keep the order using SELECT WHERE IN()?

徘徊边缘 提交于 2019-11-28 20:38:12
Is there a way to keep the order when using SELECT WHERE IN()? For example, using the following query: SELECT id FROM data_table WHERE id IN(56,55,54,1,7); The results will come back using the default order by id. 1,7,54,55,56 When I want to keep the order used in the IN: 56,55,54,1,7 Is there a quick way to do this in mySQL or will I be forced to order it after in code. Thanks :) Use FIND_IN_SET : ORDER BY FIND_IN_SET(id, '56,55,54,1,7') You can also use FIELD: ORDER BY FIELD(id, '56,55,54,1,7') http://dev.mysql.com/doc/refman/5.0/en/string-functions.html#function_field http://ivanjovanovic

SQL ORDER BY multiple columns [duplicate]

自闭症网瘾萝莉.ら 提交于 2019-11-28 20:31:50
This question already has an answer here: SQL multiple column ordering 5 answers I want to sort my products table by two columns: prod_price and prod_name . SELECT prod_id, prod_price, prod_name FROM Products ORDER BY prod_price, prod_name; How is the sorting done here? I think it happens first by prod_price and then by prod_name . Also, how is the above query different from this one: SELECT prod_id, prod_price, prod_name FROM Products ORDER BY prod_name; My products table is as follows: CREATE TABLE Products ( prod_id char(10) NOT NULL , vend_id char(10) NOT NULL , prod_name char(255) NOT

Combining ORDER BY AND UNION in SQL Server

僤鯓⒐⒋嵵緔 提交于 2019-11-28 20:03:49
How can I get first record of a table and last record of a table in one result-set? This Query fails SELECT TOP 1 Id,Name FROM Locations ORDER BY Id UNION ALL SELECT TOP 1 Id,Name FROM Locations ORDER BY Id DESC Any help? Put your order by and top statements into sub-queries: select first.Id, first.Name from ( select top 1 * from Locations order by Id) first union all select last.Id, last.Name from ( select top 1 * from Locations order by Id desc) last select * from ( SELECT TOP 1 Id,Name FROM Locations ORDER BY Id) X UNION ALL SELECT TOP 1 Id,Name FROM Locations ORDER BY Id DESC If you're