sql-order-by

ORDER BY using Criteria API

蹲街弑〆低调 提交于 2019-11-28 19:25:44
问题 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"? 回答1: You

SQL Server 2008: Ordering by datetime is too slow

我是研究僧i 提交于 2019-11-28 19:21:00
My table (SQL Server 2008) has 1 million+ records, when I try to order records by datetime, it takes 1 second, but when I order by ID (int), it only takes about 0.1 second. Is there any way to improve the efficiency? (I already added the datetime column to the index) Ordering by id probably uses a clustered index scan while ordering by datetime uses either sorting or index lookup. Both these methods are more slow than a clustered index scan. If your table is clustered by id , basically it means it is already sorted. The records are contained in a B+Tree which has a linked list linking the

how to output a standings table on the fly from a mysql table of football [soccer] results?

别说谁变了你拦得住时间么 提交于 2019-11-28 18:54:29
I have been trying to find something about this topic and I can't seem to find anything, there were a few questions on here but they didn't work for my particular project. I asked a similar question about updating the table but its not going to work for what I actually want here is the list of result. -------------------------------------------------------- |id | hometeam |goalsfor|goalsagainst| awayteam | -------------------------------------------------------- | 1 |Inter Milan | 3 | 1 | FC Barcelona | -------------------------------------------------------- | 2 |FC Barcelona | 1 | 0 | Inter

“ORDER BY … USING” clause in PostgreSQL

戏子无情 提交于 2019-11-28 18:35:48
The ORDER BY clause is decribed in the PostgreSQLdocumentation as: ORDER BY expression [ ASC | DESC | USING operator ] [ NULLS { FIRST | LAST } ] [, ...] Can someone give me some examples how to use the USING operator ? Is it possible to get an alternating order of the resultset? A.H. A very simple example would be: > SELECT * FROM tab ORDER BY col USING < But this is boring, because this is nothing you can't get with the traditional ORDER BY col ASC . Also the standard catalog doesn't mention anything exciting about strange comparison functions/operators. You can get a list of them: > SELECT

Django Query using .order_by() and .latest()

好久不见. 提交于 2019-11-28 18:11:24
I have a model: class MyModel(models.Model): creation_date = models.DateTimeField(auto_now_add = True, editable=False) class Meta: get_latest_by = 'creation_date' I had a query in my view that did the following: instances = MyModel.objects.all().order_by('creation_date') And then later I wanted instances.latest() , but it would not give me the correct instance, in fact it gave me the first instance. Only when I set order_by to -creation_date or actually removed the order_by from the query did .latest() give me the correct instance. This also happens when I test this manually using python

How to order by more than one field in Grails?

為{幸葍}努か 提交于 2019-11-28 17:47:40
Is there a way to get a list ordered by two fields, say last and first names? I know .listOrderByLastAndFirst and .list(sort:'last, first') won't work. Hates_ This old solution no longer works. Please see mattlary's answer below You may have to write a custom finder in HQL or use the Criteria Builder. MyDomain.find("from Domain as d order by last,first desc") Or def c = MyDomain.createCriteria() def results = c.list { order("last,first", "desc") } mattlary Hates_ criteria answer didn't seem to work for me; putting "last,first" in order will only cause exceptions saying, "Property 'last,first'

Sort NULL values to the end of a table

丶灬走出姿态 提交于 2019-11-28 17:18:15
问题 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 回答1: 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

What is the purpose of Order By 1 in SQL select statement?

核能气质少年 提交于 2019-11-28 17:15:39
问题 I'm reading through some old code at work, and have noticed that there are several views with an order by 1 clause. What does this accomplish? Example: Create view v_payment_summary AS SELECT A.PAYMENT_DATE, (SELECT SUM(paymentamount) FROM payment B WHERE PAYMENT_DATE = B.PAYMENT_DATE and SOME CONDITION) AS SUM_X, (SELECT SUM(paymentamount) FROM payment B WHERE PAYMENT_DATE = B.PAYMENT_DATE and SOME OTHER CONDITION) AS SUM_Y FROM payment A ORDER BY 1; 回答1: This: ORDER BY 1 ...is known as an

SQLite Query in non case sensitive alphabetical order [duplicate]

守給你的承諾、 提交于 2019-11-28 16:37:40
This question already has an answer here: How to use SQL Order By statement to sort results case insensitive? 3 answers All I want to do is grab the stuff in alphabetical order and ignore the capital letters. db.rawQuery("SELECT " + catName + " FROM "+tableName+" ORDER BY "+catName+" ASC COLLATE NOCASE;", null); This is the code I'm using above, but it always gives me an SQLite exception saying that COLLATE is a syntax error. android.database.sqlite.SQLiteException: near "COLLATE": syntax error: , while compiling: SELECT Artist FROM testTable COLLATE NOCASE ASC COLLATE goes before the order

Oracle Insert Select with order by

巧了我就是萌 提交于 2019-11-28 14:02:47
I am working on a plsql procedure where i am using an insert-select statement. I need to insert into the table in ordered manner. but the order by i used in the select sql is not working. is there any specific way in oracle to insert rows in orderly fashion? The typical use case for an ordered insert is in order to co-locate particular value in the same blocks (effectively reducing the clustering factor on indexes on columns by which you have ordered the data). This generally requires a direct path insert ... insert /*+ append */ into ... select ... from ... order by ... There's nothing