sql-order-by

Ordering a Django QuerySet by a datetime's month/day?

南笙酒味 提交于 2019-11-30 04:00:49
问题 I have a list of people, each person having a birthdate, which is predictably stored in a DateField . I'm trying to create a list of those people—sorted by the month and day of their birth (disregarding the year)—to have a sort of "who's birthday is coming up" display. I can't seem to order a QuerySet by the person's datetime.month value. Is there any way that this could be done without having to resort to coercing to a list() ? Thanks in advance and please let me know if the question needs

NHibernate HQL: left outer join with “with” clause does not work

六眼飞鱼酱① 提交于 2019-11-30 03:57:30
问题 In an EAV system, I have a mapping that looks like this: <class name="Record"> <map name="Values" table="RecordFieldValue"> <key column="RecordFK"> <index column="FieldFK"> <element column="Value"> </map> </class> I would like to select some Records, ordered by the value of each Record for a specific Field. However, note that not all Records will actually have a Value for that Field. In this case, the record should still be fetched and sorted with a null value. The desired SQL would look like

Can MySQL use index in a RANGE QUERY with ORDER BY?

我的梦境 提交于 2019-11-30 03:26:16
问题 I have a MySQL table: CREATE TABLE mytable ( id INT NOT NULL AUTO_INCREMENT, other_id INT NOT NULL, expiration_datetime DATETIME, score INT, PRIMARY KEY (id) ) I need to run query in the form of: SELECT * FROM mytable WHERE other_id=1 AND expiration_datetime > NOW() ORDER BY score LIMIT 10 If I add this index to mytable: CREATE INDEX order_by_index ON mytable ( other_id, expiration_datetime, score); Would MySQL be able to use the entire order_by_index in the query above? It seems like it

SQL ORDER BY with CASE with UNION ALL

耗尽温柔 提交于 2019-11-30 03:03:13
问题 Running PostgreSQL (7.4 and 8.x) and I thought this was working but now I'm getting errors. I can run the queries separately and it works just fine, but if I UNION or UNION ALL it throws an error. This errors out: (Warning: pg_query(): Query failed: ERROR: column "Field1" does not exist ... ORDER BY CASE "Field1" W...) SELECT "Field1" AS field_1, "Field2" AS field_2, "Field3" AS field_3, "Field4" AS field_4 FROM "TableName" WHERE condition AND other_condition UNION ALL SELECT "Field1" AS

Custom order by in SQL server like P, A, L, H [closed]

早过忘川 提交于 2019-11-30 01:34:50
Not ASC or DESC .... Order by custom... I have tried using case but not successfully SELECT * FROM Customers ORDER BY case country when 'P' then 1 … This is what I want: SELECT * FROM Customers ORDER BY case when country = 'P' then 1 when country = 'A' then 2 when country = 'L' then 3 when country = 'H' then 4 else 5 end asc 来源: https://stackoverflow.com/questions/19196475/custom-order-by-in-sql-server-like-p-a-l-h

SQL ORDER by `no` with NULLs at the end

♀尐吖头ヾ 提交于 2019-11-30 01:27:28
问题 I have got a MySql query that orders me results by no column (int, can be null). Simple example: SELECT * FROM table ORDER BY no ASC I would like to get a resultset sorted like 1, 2, 3, 10, 52, 66, NULL, NULL, NULL but I get NULL, NULL, NULL, 1, 2, 3, 10, 52, 66 Is it possible with SQL query ? 回答1: Could you try this? ORDER BY ISNULL(no),no; 回答2: You can use a CASE statement to tweak ordering: SELECT * FROM table ORDER BY case when no is null then 2 else 1 end, no This orders on "nullableness

ORDER BY using Criteria API

左心房为你撑大大i 提交于 2019-11-29 22:37:13
When I write a HQL query Query q = session.createQuery("SELECT cat from Cat as cat ORDER BY cat.mother.kind.value"); return q.list(); Everything is fine. However, when I write a Criteria Criteria c = session.createCriteria(Cat.class); c.addOrder(Order.asc("mother.kind.value")); return c.list(); I get an exception org.hibernate.QueryException: could not resolve property: kind.value of: my.sample.data.entities.Cat If I want to use Criteria and Order, how should I express my "order by"? You need to create an alias for the mother.kind . You do this like so. Criteria c = session.createCriteria(Cat

How do I order a Group result, in Linq?

时间秒杀一切 提交于 2019-11-29 22:10:19
I have the following linq query, which works fine. I'm not sure how i order the group'd result. from a in Audits join u in Users on a.UserId equals u.UserId group a by a.UserId into g select new { UserId = g.Key, Score = g.Sum(x => x.Score) } the results are currently ordered by UserId ascending. I'm after Score descending. thanks :) Arjan Einbu Just add the orderby clause ;-) from a in Audits join u in Users on a.UserId equals u.UserId group a by a.UserId into g let score = g.Sum(x => x.Score) orderby score descending select new { UserId = g.Key, Score = score }; var results = (from a in

ORDER BY NULL in MySQL

情到浓时终转凉″ 提交于 2019-11-29 21:21:43
What is ORDER BY NULL in MySQL? Does it decrease the query speed? Haim Evgi It's for performance; adding ORDER BY NULL after a GROUP BY clause will make your query faster. An explanation, from the manual : By default, MySQL sorts all GROUP BY col1, col2, ... queries as if you specified ORDER BY col1, col2, ... in the query as well. If you include an explicit ORDER BY clause that contains the same column list, MySQL optimizes it away without any speed penalty, although the sorting still occurs. If a query includes GROUP BY but you want to avoid the overhead of sorting the result, you can

Sort NULL values to the end of a table

萝らか妹 提交于 2019-11-29 21:11:29
Is there a way with PostgreSQL to sort rows with NULL values in fields to the end of the selected table? Like: SELECT * FROM table ORDER BY somevalue, PUT_NULL_TO_END Erwin Brandstetter First of all, NULL values are sorted last in default ascending order. You don't have to do anything extra. The issue applies to descending order, which is the perfect inverse and thus sorts NULL values first. The solution @Mosty pointed out was introduced with PostgreSQL 8.3 : ORDER BY somevalue DESC NULLS LAST For PostgreSQL 8.2 and older or other RDBMS without this standard SQL feature you can substitute: