sql-order-by

Conditional ORDER BY depending on column values

谁说胖子不能爱 提交于 2019-12-01 21:18:34
问题 I need to write a query that does this: SELECT TOP 1 FROM a list of tables (Joins, etc) ORDER BY Column X, Column Y, Column Z If ColumnX is NOT NULL , then at the moment, I reselect, using a slightly different ORDER BY . So, I do the same query, twice. If the first one has a NULL in a certain column, I return that row from my procedure. However, if the value isn't NULL - I have to do another identical select, except, order by a different column or two. What I do now is select it into a temp

ORDER BY datetime makes the query very slow

僤鯓⒐⒋嵵緔 提交于 2019-12-01 20:58:31
I am trying to pull data from multiple tables and when I user ORDER BY a datetime field it return the results after at least 10 seconds but if I do the same query without ORDER BY then it return the results for under 2 seconds. This is my current query SELECT ph.call_subject AS callSubject, ac.account_name AS accountName, DATE_FORMAT(ph.trigger_on, "%c/%e/%Y %h:%i %p") AS triggerOn, ind.name AS industry, cc.call_code_name AS callCode FROM phone_calls AS ph INNER JOIN accounts AS ac ON ph.account_id = ac.account_id INNER JOIN industries AS ind ON ind.industry_id = ac.industry_id INNER JOIN call

Sorting Alphanumeric field in SQL CE (Compact Edition) version 3.5

我只是一个虾纸丫 提交于 2019-12-01 20:36:56
Sorting Alphanumeric field in SQL CE (Compact Edition) version 3.5 TreeNumber is a nvarchar field with a mix of numbers and strings for the values. I want to sort these records so that the records that contain alpha characters are at the top and the rest are sorted in numeric order. I want something similar to the following query which works in SQL Server: SELECT * FROM Tree ORDER BY (CASE WHEN TreeNumber LIKE '%[a-z]%' THEN 0 ELSE TreeNumber END), TreeNumber The above query doesn't seem to work because the [] range is not supported in CE. Another solution which works with SQL Server but doesn

ORDER BY separately positive & negative numbers in MySQL statement

孤者浪人 提交于 2019-12-01 19:58:02
问题 I have a MySQL table and would like to return the rows based on a column value in this order: First ascending order if the column >=0 Then descending order if the column <0 E.g. 0,2,4,7,-2,-3,-5 回答1: Can use SIGN to sort the positive numbers to the top, then take the absolute value with ABS to get the desired ASC/DESC. SELECT * FROM theTable ORDER BY SIGN(col) DESC, ABS(col) EDIT As Nahuel pointed out, the above will sort 0's to the middle between positive and negative. To instead group them

Sort by Date in SQL

无人久伴 提交于 2019-12-01 19:34:19
I have a resources table, one of the fields is a date field with the Data Type of date. I want to have to following output: Current month records (say May - year is not important) Then the following (again, assuming May is the current month) June Records July Records August Records September Records October Records November Records December Records January Records February Records March Records April Records Come June, June is the current month and then the order would be: July Records August Records ... Here is my SQL...I don't know how to ORDER the output to achieve the desired order (5,6,7

toPagedList() order differs from the LINQ order by clause

泪湿孤枕 提交于 2019-12-01 18:44:36
I have a small application that displays a list of shortURLs to a user. I'm using ASP.NET MVC3, Entity Framework, and an Oracle backend. I'm also using the PagedList library by Troy Goode ( https://github.com/TroyGoode/PagedList ) I want the user to see the very last entry first, similar to how blog comments are ordered. To do this I added an "orderby descending" clause to the LINQ statement: var links = from l in db.LINKS orderby l.ID descending select l; In debug the "links" object is ordered as expected (in descending order, by the primary key "ID"). Now I want to pass this list of links to

Conditional ORDER BY depending on column values

别来无恙 提交于 2019-12-01 18:40:37
I need to write a query that does this: SELECT TOP 1 FROM a list of tables (Joins, etc) ORDER BY Column X, Column Y, Column Z If ColumnX is NOT NULL , then at the moment, I reselect, using a slightly different ORDER BY . So, I do the same query, twice. If the first one has a NULL in a certain column, I return that row from my procedure. However, if the value isn't NULL - I have to do another identical select, except, order by a different column or two. What I do now is select it into a temp table the first time. Then check the value of the column. If it's OK, return the temp table, else, redo

MySQL order by two values

泄露秘密 提交于 2019-12-01 18:37:21
问题 In short, I have a game of sorts, and the score board for it shows the score of the player at the end + how long they took to achieve it. I need to order my MySQL results so that the highest score in the fastest time is first and the lowest score with the slowest time is last, and everything in between is ranked in the same way. But I have no idea how I'd do that, here's my sql as it stands: $sql1 = "SELECT * FROM boobmatch_highscores ORDER BY t_score DESC, time ASC"; Any help would be

MySQL order by two values

故事扮演 提交于 2019-12-01 18:08:56
In short, I have a game of sorts, and the score board for it shows the score of the player at the end + how long they took to achieve it. I need to order my MySQL results so that the highest score in the fastest time is first and the lowest score with the slowest time is last, and everything in between is ranked in the same way. But I have no idea how I'd do that, here's my sql as it stands: $sql1 = "SELECT * FROM boobmatch_highscores ORDER BY t_score DESC, time ASC"; Any help would be greatly appreciated! The data in the table: Score Time 23 00:04:32 23 00:04:31 25 00:02:32 16 00:06:32 35 00

Does ORDER BY apply before or after DISTINCT?

孤人 提交于 2019-12-01 17:43:29
问题 In a MySQL query, when using the DISTINCT option, does ORDER BY apply after the duplicates are removed? If not, is there any way to make it do so? I think it's causing some issues with my code. EDIT : Here's some more information about what's causing my problem. I understand that, at first glance, this order would not be important, since I am dealing with duplicate rows. However, this is not entirely the case, since I am using an INNER JOIN to sort the rows. Say I have a table of forum