sql-order-by

Is there a way in SQL (MySQL) to do a “round robin” ORDER BY on a particular field?

混江龙づ霸主 提交于 2020-01-21 03:25:21
问题 Is there a way in SQL (MySQL) to do a "round robin" ORDER BY on a particular field? As an example, I would like to take a table such as this one: +-------+------+ | group | name | +-------+------+ | 1 | A | | 1 | B | | 1 | C | | 2 | D | | 2 | E | | 2 | F | | 3 | G | | 3 | H | | 3 | I | +-------+------+ And run a query that produces results in this order: +-------+------+ | group | name | +-------+------+ | 1 | A | | 2 | D | | 3 | G | | 1 | B | | 2 | E | | 3 | H | | 1 | C | | 2 | F | | 3 | I |

Implementing NULLS FIRST in Amazon Redshift

空扰寡人 提交于 2020-01-17 03:13:05
问题 I am using sum window function for row number, similar to this query - SELECT field_a, SUM(1) OVER (PARTITION BY field_b ORDER BY field_c ASC ROWS UNBOUNDED PRECEDING) AS row_number FROM test_table ORDER BY row_number; The problem is that if field_c is a null value, it appears at the end. I want it at the beginning, so null value is treated as smaller than all other values. In Oracle, this could be done by providing NULLS FIRST argument, but its not supported in Redshift. So how do I

SQL Query: Fetch ordered rows from a table

二次信任 提交于 2020-01-17 03:05:13
问题 Following are some entries from a table: id r_id a_id p_id 1 9 9 0 2 9 105 108 3 9 102 9 4 9 106 105 5 9 108 102 Is it possible to get the following output using SQL query 1 9 9 0 3 9 102 9 5 9 108 102 2 9 105 108 4 9 106 105 The idea is to sort the rows in such a way that a row with p_id = x should come below the row with a_id = x. I hope question makes sense. Regards, Mayank EDIT: I'm looking this for PostgreSql The root item has a p_id = 0 There are no missing links 回答1: Use a recursive

Using ORDER BY with UNION and ALIASES with complex query

十年热恋 提交于 2020-01-16 09:24:31
问题 I want to sort Order No of the following query with UNION but it could not be done although googling a lot. IF LEN(@_Order_No)=0 BEGIN SELECT a._PROCESS_INST_NO,c._ISSUE_DATE,c._DELIVERY_DATE, a._Order_No,a._GOODS_CD,a._GOODS_NAME , --a._QTY, CAST(a._QTY as DECIMAL (38,2)) as _QTY, b._GOODS_CD as RM_CD,b._GOODS_NAME as RM_NAME,b._UNIT, (CASE WHEN LEFT(a._GOODS_NAME,2)='SS' OR LEFT(a._GOODS_NAME,2)='SN' THEN CAST(a._QTY as DECIMAL (38,2)) * CAST(b._QTY as DECIMAL (38,2))/ NULLIF(SUM(CAST(a.

Using ORDER BY with UNION and ALIASES with complex query

匆匆过客 提交于 2020-01-16 09:24:17
问题 I want to sort Order No of the following query with UNION but it could not be done although googling a lot. IF LEN(@_Order_No)=0 BEGIN SELECT a._PROCESS_INST_NO,c._ISSUE_DATE,c._DELIVERY_DATE, a._Order_No,a._GOODS_CD,a._GOODS_NAME , --a._QTY, CAST(a._QTY as DECIMAL (38,2)) as _QTY, b._GOODS_CD as RM_CD,b._GOODS_NAME as RM_NAME,b._UNIT, (CASE WHEN LEFT(a._GOODS_NAME,2)='SS' OR LEFT(a._GOODS_NAME,2)='SN' THEN CAST(a._QTY as DECIMAL (38,2)) * CAST(b._QTY as DECIMAL (38,2))/ NULLIF(SUM(CAST(a.

Customised Ordering in SQL

Deadly 提交于 2020-01-15 11:36:11
问题 I have a SQL query like SELECT column1,column2 FROM table1 WHERE column1 IN('q1','q2','q3') The results are shown in the order: q1 q2 q3 If I change the query to SELECT column1,column2 FROM table1 WHERE column1 IN('q3','q1','q2') Still the order remains same. How to achieve this ordering in the sql statement? Help!! Thanks 回答1: One way to solve this is to add an extra column in your table called for example 'SortOrder' that contains integers determining in which order the rows should be

how to sort varchar numeric columns by DESC or ASC?

主宰稳场 提交于 2020-01-15 05:34:26
问题 I write ... ORDER BY column ASC but my column is VARCHAR and it sorts wrong like 1, 10, 2 , instead of 1, 2, 10 . How can I do it to sort like 1, 2, 10 ? 回答1: order by cast(column as float) Notes: Assumed you only have numbers in the columns. No "fish" or "bicycle" empty strings CAST to zero Edit: For MySQL. You can not cast to float order by cast(column as decimal(38,10)) 回答2: You can cast to int... order by cast(column as int) DEMO DECLARE @q as table( name varchar(50), columnn varchar(10)

Row_Number() CTE Performance when using ORDER BY CASE

六月ゝ 毕业季﹏ 提交于 2020-01-15 04:53:25
问题 I have a table I'd like to do paging and ordering on and was able to get a query similar to the following to do the work (the real query is much more involved with joins and such). WITH NumberedPosts (PostID, RowNum) AS ( SELECT PostID, ROW_NUMBER() OVER (ORDER BY CASE WHEN @sortCol = 'User' THEN User END DESC, CASE WHEN @sortCol = 'Date' THEN Date END DESC, CASE WHEN @sortCol = 'Email' THEN Email END DESC) as RowNum FROM Post ) INSERT INTO #temp(PostID, User, Date, Email) SELECT PostID, User

Row_Number() CTE Performance when using ORDER BY CASE

空扰寡人 提交于 2020-01-15 04:53:13
问题 I have a table I'd like to do paging and ordering on and was able to get a query similar to the following to do the work (the real query is much more involved with joins and such). WITH NumberedPosts (PostID, RowNum) AS ( SELECT PostID, ROW_NUMBER() OVER (ORDER BY CASE WHEN @sortCol = 'User' THEN User END DESC, CASE WHEN @sortCol = 'Date' THEN Date END DESC, CASE WHEN @sortCol = 'Email' THEN Email END DESC) as RowNum FROM Post ) INSERT INTO #temp(PostID, User, Date, Email) SELECT PostID, User

Ampersand (&) character is being ignored in Oracle ORDER BY

拥有回忆 提交于 2020-01-14 16:39:13
问题 I am running a query in Oracle and that query is ordered by a column that may have values with ampersand. However, it seems that ampersand is being ignored by the sorting algorithm. For instance: select * from ( select '&' txt from dual union select 'P' txt from dual union select 'N' txt from dual ) order by txt Prints exactly (and correctly, I guess): & N P However, if I change the text with '&' to '&Z', the result changes: select * from ( select '&'||'Z' txt from dual // concatenating just