sql-order-by

Django: how to order_by on a related field of a related field

谁说我不能喝 提交于 2019-12-07 02:32:00
问题 I'm using annotate to add a property to an object which I can then use for order_by. However, I want to annotate on a field of a relation on a relation. I know I should be able to get to the field somehow using double-underscore notation, but I just can't seem to wrap my head around it. Here are the models: class Group(Taggable, Uploadable): name = models.CharField(max_length=250, db_index=True) description = models.TextField(max_length=5000, null=True, blank=True, db_index=True) private =

LInq Order By and Order By Desc

家住魔仙堡 提交于 2019-12-07 01:50:47
问题 I am using "Linq" to filter list of objects and to sort them, like myList.Where(x => x.Item!= "SF" && x.AdSize == minadSize) .OrderBy(x => x.ManufacturingDate) .OrderBy(x=>x.ExpiryDate); I doubt whether i am doing it right or not that is if i want to "sorting" on multiple fields then is it necessary to use multiple Order By clause cant it be done with single "OrderBy" 回答1: Don't use multiple OrderBy calls - use OrderBy followed by ThenBy : var query = myList.Where(x => x.Item!= "SF" && x

How to sort a collection based on a subcollection property

空扰寡人 提交于 2019-12-07 01:48:34
问题 I would like to sort a collection based on a subcollection property. //the subcollection public class Salary { public int SalaryId {get;set;} public int SalaryYear {get;set;} public double SalaryValue {get;set;} //this is the field we want to sort the parent collection "Person" } //the main collection public class Person { public int PersonId {get;set;} public string PersonName {get;set;} public List<Salary> Salaries {get;set;} } Below just for testing purpose, I'm preparing my collection of

GROUP BY ignoring an attribute

大憨熊 提交于 2019-12-06 21:33:45
问题 for example i have this table: itemgroup | description | price A, a, 10 A, b, 12 A, c, 14 B, g, 11 B, h, 16 now i want to select the rows with the highest price in one group like this: A, c, 14 B, h, 16 The SQL query (is fully functional) wich gets me near this is: SELECT itemgroup, MAX( price ) FROM table GROUP BY itemgroup A, 14 B, 16 By trying this I get an "not a GROUP BY expression"-error: SELECT itemgroup, description, MAX( price ) FROM table GROUP BY itemgroup I need something like

Prevent SQL Injection in ORDER BY clause

自闭症网瘾萝莉.ら 提交于 2019-12-06 20:20:41
问题 In our DB access layer we have some dynamic query creation. For instance, we have the following method for building a part of an ORDER BY clause: protected string BuildSortString(string sortColumn, string sortDirection, string defaultColumn) { if (String.IsNullOrEmpty(sortColumn)) { return defaultColumn; } return String.Format("{0} {1}", sortColumn, sortDirection); } The problem is, sortColumn and sortDirection both come from outside as strings, so of course something should be done to

Is PostgreSQL order fully guaranteed if sorting on a non-unique attribute? [duplicate]

人盡茶涼 提交于 2019-12-06 18:01:48
问题 This question already has answers here : Closed 7 years ago . Possible Duplicate: Why do results from a SQL query not come back in the order I expect? From reading 7.5 Sorting Rows and from issues I've seen with PostgreSQL, my impression is the following, but that section is not fully explicit, so I would be grateful if someone could verify: SELECT * FROM items; has no guaranteed order. SELECT * FROM items ORDER BY published_date ASC; guarantees that two items with different dates come in a

How to improve query performance with order by, group by and joins

做~自己de王妃 提交于 2019-12-06 15:27:42
I Had a problem with order by when joins multiple tables which have millions of data. But I got solution as instead of join with distinct use of EXISTS will improve performance from the following question How to improve order by performance with joins in mysql SELECT `tracked_twitter` . *, COUNT( * ) AS twitterContentCount, retweet_count + favourite_count + reply_count AS engagement FROM `tracked_twitter` INNER JOIN `twitter_content` ON `tracked_twitter`.`id` = `twitter_content`.`tracked_twitter_id` INNER JOIN `tracker_twitter_content` ON `twitter_content`.`id` = `tracker_twitter_content`.

Order against two columns at the same time (intersecting)

被刻印的时光 ゝ 提交于 2019-12-06 15:01:50
问题 I have a table with the fields CommonName and FirstName . Only either field has data, never both. Is there a way to order rows in an intersecting manner on SQL Server? Example: CommonName FirstName Bern Wade Ashley Boris Ayana I want records ordered like this: CommonName FirstName Ashley Ayana Bern Boris Wade Is this possible, and if so, how? 回答1: Use a CASE statement to select the value for that row and ORDER BY that. 回答2: ORDER BY CASE WHEN CommonName is null THEN FirstName ELSE CommonName

Join two tables, then Order By date, BUT combining both tables

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-06 13:56:55
Alright, I'm trying to figure out why I can't understand how to do this well... I have two tables: invoices : id userID amount date payments : id userID amount date So, the goal here is to join both tables, where the userID matches whatever I want it to be - and then return everything ordered by date (most recent at the top). However, because there is a date field in each of the tables, I'm not sure how MySQL will handle things... will is sort by both dates automatically? Here's what I was thinking... "SELECT DISTINCT * FROM invoices,payments WHERE {$userID} = invoice.userID OR {$userID} =

Retain ordering from IN subquery

送分小仙女□ 提交于 2019-12-06 13:03:46
this should be an easy one, but I can't seem to get mysql to play ball. I'm trying to build a list of projects that a user most recently voted for, to be used on that users profile page. I have table of votes, containing uid(INT), project(INT), timestamp(INT) And a table of projects whose id field matches the project field in the votes table. My initial query was SELECT * FROM projects WHERE id IN(SELECT project FROM votes WHERE uid=x ORDER BY timestamp DESC); This gives a correct list, but the ordering is messed up. I also realised that I had forgotten to add DISTINCT project to the subquery,