sql-order-by

A specific value(only one) to be first in a query result. Rest order by time stamp

て烟熏妆下的殇ゞ 提交于 2020-01-06 03:11:13
问题 I have a search result query for posts. And I want posts written by myself(userId = 27) to be first in query result and rest ordered by time stamp. Can anyone give me query for that in mysql? 回答1: select * from posts order by if (userid=27, -1, any_timestamp_include_zero); include your full table schema help much better 回答2: How about something like: select * from post order by case when userid = 25 then '0001-01-01 00:00:00' else my_timestamp end (formatting the '0001-01-01' part

Best way to fetch last 4 rows from a result set using mysql

≯℡__Kan透↙ 提交于 2020-01-06 02:28:34
问题 Can any one please let me know, that, i need to fetch last 4 rows from a result-set using mysql. The result-set returns totally 6 records. but, i need the records to be fetch from last4...i.e, Record-3 Record-4 Record-5 Record-6 回答1: To get the last x number of rows, but have them returned in ascending order, use: SELECT x.value FROM (SELECT y.value FROM TABLE y ORDER BY y.value DESC LIMIT 4) x ORDER BY x.value The answer requires that you create a derived table (AKA inline view) based on the

Passing an IComparer parameter to custom LINQ OrderBy extension method

陌路散爱 提交于 2020-01-05 05:39:06
问题 After a good dose of Googling and trying some things and not finding/getting the desired result I decided to post this question. I have a custom made OrderBy extension method and now when performing the OrderBy operation I'd like to pass an AlphanumComparator like this: return divergences.OrderBy(sort, new AlphanumComparator()); Here's the extension method: public static IQueryable<T> OrderBy<T>(this IQueryable<T> collection, GridSortOptions sortOptions, AlphanumComparator comparer = null) {

SQL ORDER BY (sequence) [duplicate]

醉酒当歌 提交于 2020-01-05 04:40:33
问题 This question already has answers here : sql ORDER BY multiple values in specific order? (8 answers) Closed 5 years ago . I have a sql statement which I would want to ORDER BY a specific sequence. SELECT * FROM UserDB ORDER BY [Role] How can I make it such that the data brought to my GridView table is listed from Admin on the top, follow by User and Guests? 回答1: So you want to order by Admin/User/Guest ? Try something like : SELECT * FROM UserDB ORDER BY CASE Role WHEN 'Admin' THEN 0 WHEN

Dense_Rank ordering

痞子三分冷 提交于 2020-01-05 04:21:13
问题 I've read a few variations of this question, but the solutions don't seem to be working. I wish to dynamically create a "Subgroup" for each "OrderNo" & "GroupID". Subgroups should be ordered by "OrderLine" eg: (Expected outcome) OrderNo OrderLine GroupID Subgroup ------------------------------------ 10463 1 798 1 10463 2 799 2 10463 3 797 3 10463 5 65 4 10463 6 65 4 10463 7 65 4 10481 4 917 1 10481 5 918 2 10481 6 131 3 10481 7 131 3 10481 8 131 3 10481 9 130 4 I've used Dense_Rank() to

Order rows according to which condition is met?

烂漫一生 提交于 2020-01-05 02:52:06
问题 I have a pretty simple question: Is it possible to order the rows retrieved according to the which condition is met? For example, I have a table of people, and I want to retrieve all people whose names begin with an "I", or end with an "ster", or contain "lo", ordered according to which condition of these is met. First the rows that match the first condition, then the rows that match the second, and so on. ( Without duplicated: if a row meets the first condition, it shouldn't show again for

Order rows according to which condition is met?

时光毁灭记忆、已成空白 提交于 2020-01-05 02:51:32
问题 I have a pretty simple question: Is it possible to order the rows retrieved according to the which condition is met? For example, I have a table of people, and I want to retrieve all people whose names begin with an "I", or end with an "ster", or contain "lo", ordered according to which condition of these is met. First the rows that match the first condition, then the rows that match the second, and so on. ( Without duplicated: if a row meets the first condition, it shouldn't show again for

Oracle Order by not working for Subquery from DUAL

*爱你&永不变心* 提交于 2020-01-04 06:05:43
问题 Hi all when I executed this query somehow its throwing the following error - ORA-00907: missing right parenthesis . But if you remove the order by 1 from SELECT 2 FROM DUAL order by 1 its working. Did I miss something out here or its ORACLE limitation SELECT (CASE WHEN EXISTS (SELECT 1 FROM DUAL) THEN (SELECT 4 FROM dual) ELSE (SELECT 2 FROM DUAL order by 1 ) END) AS DELEGATOR FROM dual Below is a working code with order by 1 removed SELECT (CASE WHEN EXISTS (SELECT 1 FROM DUAL) THEN (SELECT

Multiple columns in OVER ORDER BY

爷,独闯天下 提交于 2020-01-03 08:43:08
问题 Is there a way to specify multiple columns in the OVER ORDER BY clause? SELECT ROW_NUMBER() OVER(ORDER BY (A.Col1)) AS ID FROM MyTable A The above works fine, but trying to add a second column does not work. SELECT ROW_NUMBER() OVER(ORDER BY (A.Col1, A.Col2)) AS ID FROM MyTable A Incorrect syntax near ','. 回答1: The problem is the extra parentheses around the column name. These should all work: -- The standard way SELECT ROW_NUMBER() OVER(ORDER BY A.Col1) AS ID FROM MyTable A SELECT ROW_NUMBER

get first record in group by result base on condition

 ̄綄美尐妖づ 提交于 2020-01-03 05:45:09
问题 this is my database structure create database testGroupfirst; go use testGroupfirst; go create table testTbl ( id int primary key identity,name nvarchar(50) ,year int ,degree int , place nvarchar(50) ) insert into testTbl values ('jack',2015,50,'giza') insert into testTbl values ('jack',2016,500,'cai') insert into testTbl values ('jack',2017,660,'alex') insert into testTbl values ('jack',2018,666,'giza') insert into testTbl values ('jack',2011,50,'alex') insert into testTbl values ('rami'