sql-order-by

Order a List (C#) by many fields? [duplicate]

北慕城南 提交于 2019-11-28 04:26:42
This question already has an answer here: Multiple Order By with LINQ [duplicate] 1 answer I want to order a List of objects in C# by many fields, not just by one. For example, let's suppose I have a class called X with two Attributes, A and B, and I have the following objects, in that order: object1 => A = "a", B = "h" object2 => A = "a", B = "c" object3 => A = "b", B = "x" object4 => A = "b", B = "b" and I want to order the list by A attribute first, and when they are equals, by B element, so the order would be: "a" "c" "a" "h" "b" "b" "b" "x" As far as I know, the OrderBy method order by

How to sort inner list that is returned by entity framework?

孤人 提交于 2019-11-28 04:21:12
问题 How do I sort the inner collection of an object returned by the entity framework? public class X { public string Field {get; set;} public EntityCollection<Y> Ys {get; set;} } public class Y { public string Field {get; set;} } from x in entities.Xs orderby x.Field select x Is there a way to modify this LINQ query to return the X objects and also have the Y objects sorted? Or do I have to manually sort the Y list when it comes back? EDIT: This code must return a collection of X typed objects,

mysql order by, null first, and DESC after

怎甘沉沦 提交于 2019-11-28 04:16:22
How can I order DESC by a field, but list the NULL values first? So I'm having a table: reuestId | offerId | offerTitle 1 | 1 | Alfa NULL | 2 | Beta 2 | 3 | Gamma I want to select them so that the results would be: NULL | 2 | Beta 2 | 3 | Gamma 1 | 1 | Alfa Try this: ORDER BY [reuestId] IS NULL DESC, [reuestId] DESC should work (for mySql) ypercubeᵀᴹ SELECT * FROM TableX ORDER BY (requestId IS NOT NULL) , requestId DESC 来源: https://stackoverflow.com/questions/9307613/mysql-order-by-null-first-and-desc-after

How to select ORDER BY column and RAND() both?

孤者浪人 提交于 2019-11-28 03:57:00
问题 Hello dear friends. mysql_query("SELECT id FROM tb_table ORDER BY num ASC, ORDER BY RAND() LIMIT 1"); is this coding correct? I want to find all rows ASC num and there can be 1000 rows that num is 1 another 1000 that num is 2. But I want it to sort ASC like 1s firstly and choose one of them randomly. 回答1: You only need to specify ORDER BY once. mysql_query("SELECT id FROM tb_table ORDER BY num ASC, RAND() LIMIT 1"); 来源: https://stackoverflow.com/questions/7920574/how-to-select-order-by-column

Django : Order by position ignoring NULL

℡╲_俬逩灬. 提交于 2019-11-28 03:56:04
I have a problem with django queryset ordering. My model contains a field named position (a PositiveSmallIntegerField ), which I'd like to used to order query results. I use order_by('position') , which works great. Problem : my position field is nullable ( null=True, blank=True ), because I don't wan't to specify a position for every 50000 instances of my model :( When some instances have a NULL "position", order_by returns them in the top of the list : I'd like them to be at the end... In RAW SQL, I used to write things like " IF(position IS NULL or position='', 1, 0) " (see http://www

OrderBy in SQL Server to put positive values before negative values

僤鯓⒐⒋嵵緔 提交于 2019-11-28 03:49:23
问题 I have table in SQL Server which contains a column of type "int". The column can contain positive as well as negative values. I want to carry out sorting based on this column values such that rows with positive values in this column come before the negative values. Example: Code SortColumn A 1 B 5 C -1 D -3 E 0 F 2 Desired Output: Code SortColumn E 0 A 1 F 2 B 5 C -3 D -1 回答1: Select * from Table order by Case when sortcolumn<0 then 1 else 0 end ,sortcolumn 来源: https://stackoverflow.com

ORDER BY … COLLATE in SQL Server

此生再无相见时 提交于 2019-11-28 03:44:19
问题 Under SQL Server. A table contains some text with different cases. I want to sort them case-sensitive and thought that a COLLATE in the ORDER BY would do it. It doesn't. Why? CREATE TABLE T1 (C1 VARCHAR(20)) INSERT INTO T1 (C1) VALUES ('aaa1'), ('AAB2'), ('aba3') SELECT * FROM T1 ORDER BY C1 COLLATE Latin1_General_CS_AS SELECT * FROM T1 ORDER BY C1 COLLATE Latin1_General_CI_AS Both queries return the same, even if the first one is "CS" for case-sensitive aaa1 AAB2 aba3 (in the first case, I

MySQL conditional ORDER BY ASC/DESC for date column

此生再无相见时 提交于 2019-11-28 03:40:33
问题 I need a MySQL conditional ORDER BY statement for a datetime field. I have a table with posts which I would like to order in the following way: all future posts should be ordered ASC and all historical posts ordered DESC . Eg.: post_status post_date post_title =========== ========= ========== future 2012-10-01 Title 1 future 2012-12-01 Title 2 publish 2012-05-01 Title 3 publish 2012-01-01 Title 4 I need something similar to the following SQL... SELECT post_status, post_date, post_title FROM

Custom Order in Oracle SQL

老子叫甜甜 提交于 2019-11-28 03:27:16
I need to order transaction based on the currency. However I need to implement a custom order by, which makes the USD to always comes on the top, and the rest should be ordered asc. for example : BHT USD MYR JYP should be sorted like : USD BHT JPY MYR Is there a simple way to handle this? Don't know if this qualifies as simple: order by case when currency = 'USD' then 1 when currency = 'BHT' then 2 when currency = 'JPY' then 3 when currency = 'MYR' then 4 else 5 end or a bit more compact but Oracle specific: order by decode(currency, 'USD', 1, 'BHT', 2, 'JPY', 3, 'MYR', 4, 5) The above

How can I sort by the id of a ManyToManyField in Django?

折月煮酒 提交于 2019-11-28 03:17:06
问题 I've got a ManyToManyField in a user object and it's used to map the users that user is following. I'm trying to show a subset list of who they have most recently followed. Is there a trick in .order_by() that will allow me to sort by the id of the ManyToManyField? The data is there, right? # (people the user is following) following = models.ManyToManyField(User, related_name="following", blank=True) theuser.following.filter(user__is_active=True).order_by("user__id") That will give me a list