sql-order-by

Sorting range of list using LINQ

Deadly 提交于 2020-01-03 03:31:11
问题 I was wondering if it is possible to do ranged sort using LINQ, for example i have list of numbers: List< int > numbers = new List< int > 1 2 3 15 <-- sort 11 <-- sort 13 <-- sort 10 <-- sort 6 7 etc. Simply using numbers.Skip(3).Take(4).OrderBy(blabla) will work, but it will return a new list containing only those 4 numbers. Is is somehow possible to force LINQ to work on itself without returning a new "partial" list or to receive complete one with sorted part? Thanks for any answer! 回答1:

Indexing on a field and “order by” on another field

六月ゝ 毕业季﹏ 提交于 2020-01-02 15:08:16
问题 Let's assume I have a small database with three columns : "id1", "id2" and "date". I am indexing my database on the field "id1" since it is a frequently selected field (many select queries are ran with ex: where "id1" = 125548). In some particular query I have, I need to sort the records of a user based on the date" field in the table which is not indexed. My curiosity is if the sort operation (which is basically a order-by operation) on the date field will be ran on the whole database or

Django order items by two fields, but ignoring them if they're zero

牧云@^-^@ 提交于 2020-01-01 08:05:18
问题 I have the following model (greatly simplified for the purposes of this question): class Product(models.Model): price = models.DecimalField(max_digits=8, decimal_places=2) sale_price = models.DecimalField(max_digits=10, blank=True, null=True, decimal_places=2) For the majority of products, price will be filled but sale_price will not be. So, I can order products by price like so: Product.objects.order_by('price') Product.objects.order_by('-price') However, some products will have a sale_price

MySQL - Select most recent date out of a set of several possible timestamps?

走远了吗. 提交于 2020-01-01 04:16:19
问题 I would like to know if this is possible using a mysql query: I have a table called updates. In the updates table I have 2 columns, id and timestamp . I have 5 rows each with the same id but with different date timestamps. An example of this timestamp would be the value: 2012-08-04 23:14:09 . I would like to select only the most recent timestamp value out of the 5 results. This could also be explained by saying that I would like to select the value that is closest to the current date and time

How to sort results by string length on MongoDB

左心房为你撑大大i 提交于 2020-01-01 02:30:08
问题 I can do it easily on mysql select * from TABLE order by length(FIELD) asc How can I do it on MongoDB? 回答1: MongoDB 3.4 introduces the $strLenCP aggregation operator that finally supports this. An example: db.collection.aggregate( [ {$project: { "field": 1, "field_length": { $strLenCP: "$field" } }}, {$sort: {"field_length": -1}}, {$project: {"field_length": 0}}, ] ) 回答2: To sort documents in MongoDB, you need to use sort() method. The method accepts a document containing a list of fields

OrderBy not having any effect in LINQ

冷暖自知 提交于 2019-12-31 05:09:47
问题 I have a table that has a set of data in it, as follows: Notice the above results are gathered by the following SQL query: select * from Logs where RegisterationId = 16 and date = '2018-04-13 00:00:00.000' order by DateTime ASC; Now this is perfect but when I try to do the same in LINQ using: var logs = db.Logs.Where(x => x.RegisterationId == EnrollNumber && x.Date >= StartDate && x.Date <= EndDate && x.isIgnore != true).OrderBy(x => x.DateTime).Distinct().ToList(); it gives all the Manual

How to provide the sort order in a variable in sqlalchemy?

纵饮孤独 提交于 2019-12-31 02:00:46
问题 Right now what I am doing is as : if order_type == 'desc': result = session.\ query(Customer).\ order_by(desc(getattr(Customer, sorting_column_name))).\ all() else: result = session.\ query(Customer).\ order_by(asc(getattr(Customer, sorting_column_name))).\ all() Is there any way to call order_by just once and use sorting order provided in order_type as a variable to decide whether to sort asc or desc ? 回答1: asc and desc are just objects, pick one based on the ordering you want: direction =

MySQL Query ORDER BY certain values before others

a 夏天 提交于 2019-12-30 22:59:09
问题 I am developing a game highscore where the time of completion is stored in a database, but if you don't complete the level but save your highscore, the completion time is 0. I am trying to make a query which lists the highscore with the lowest completion time first, but my simple ORDER BY is sticking all of the 0 completion times at the start of course! Is there any way I can order it like this, but have the 0's at the bottom. I have tried: SELECT FROM highscores ORDER BY completiontime > 0,

Natural Sorting SQL ORDER BY

余生颓废 提交于 2019-12-30 10:30:03
问题 Can anyone lend me a hand as to what I should append to my ORDER BY statement to sort these values naturally: 1 10 2 22 20405-109 20405-101 20404-100 X Z D Ideally I'd like something along the lines of: 1 2 10 22 20404-100 20405-101 20405-109 D X Z I'm currently using: ORDER BY t.property, l.unit_number where the values are l.unit_number I've tried doing l.unit_number * 1 and l.unit_number + 0 but they haven't worked. Should I be doing sort of ORDER conditional, such as Case When IsNumeric(l

Select last n rows without use of order by clause

限于喜欢 提交于 2019-12-30 09:57:28
问题 I want to fetch the last n rows from a table in a Postgres database. I don't want to use an ORDER BY clause as I want to have a generic query. Anyone has any suggestions? A single query will be appreciated as I don't want to use FETCH cursor of Postgres. 回答1: That you get what you expect with Lukas' solution (as of Nov. 1st, 2011) is pure luck . There is no "natural order" in an RDBMS by definition. You depend on implementation details that could change with a new release without notice. Or a