sql-order-by

Indexed ORDER BY with LIMIT 1

你离开我真会死。 提交于 2019-11-29 13:54:47
I'm trying to fetch most recent row in a table. I have a simple timestamp created_at which is indexed. When I query ORDER BY created_at DESC LIMIT 1 , it takes far more than I think it should (about 50ms on my machine on 36k rows). EXPLAIN -ing claims that it uses backwards index scan , but I confirmed that changing the index to be (created_at DESC) does not change the cost in query planner for a simple index scan . How can I optimize this use case? Running postgresql 9.2.4 . Edit: # EXPLAIN SELECT * FROM articles ORDER BY created_at DESC LIMIT 1; QUERY PLAN -----------------------------------

ORDER BY in a Sql Server 2008 view

匆匆过客 提交于 2019-11-29 13:19:34
we have a view in our database which has an ORDER BY in it. Now, I realize views generally don't order, because different people may use it for different things, and want it differently ordered. This view however is used for a VERY SPECIFIC use-case which demands a certain order. (It is team standings for a soccer league.) The database is Sql Server 2008 Express, v.10.0.1763.0 on a Windows Server 2003 R2 box. The view is defined as such: CREATE VIEW season.CurrentStandingsOrdered AS SELECT TOP 100 PERCENT *, season.GetRanking(TEAMID) RANKING FROM season.CurrentStandings ORDER BY GENDER,

Query Sqlite Database by specific/custom ordering?

∥☆過路亽.° 提交于 2019-11-29 12:24:39
Let's say I got a table, something like this: ID | TITLE 1 | AAA 2 | BBB 3 | CCC 4 | DDD 5 | EEE ... ... I want to perform a query, using IN opeator, while preserving the order of the IN Arguments database.query("some_table", null, ID + " IN("+ idsStr+")", null, null, null, null); For example, if the query is "select from some_table where id in (4,1,5) ...(order by???)", I want that the returned cursor to be sorted as 4,1,5 Is it possible? how? EDIT: Tested, works: SELECT * FROM books WHERE _id IN(4, 1, 5) ORDER BY CASE _id WHEN '4' THEN 1 WHEN '1' THEN 2 WHEN '5' THEN 3 ELSE 4 END, _id; I

Is it possible to select a specific ORDER BY in SQL Server 2008?

泪湿孤枕 提交于 2019-11-29 12:16:09
问题 I have a table that holds days and times, the day column, can have any of the seven days entered into it, and they are set to data type varchar . As this table holds booking times for a client, I want to select all days from the table where the id matches, and I want to sort by day Monday-Sunday. I was hoping that I could add something to this query to manually select the order the results come back like so: select * from requirements where Family_ID = 1 ORDER BY Day, Monday, Tuesday,

MySQL - using GROUP BY and DESC

三世轮回 提交于 2019-11-29 12:15:52
问题 In my SQL query I am selecting data with GROUP BY and ORDER BY clauses. The table has the same numbers across multiple rows with different times in each row. So I think I want to apply a GROUP BY clause. However in the results return the oldest time with the number, but I need the most recent time. SELECT * FROM TABLE GROUP BY (numbers) ORDER BY time DESC The query appears as if it should first apply GROUP BY and then ORDER BY ... but the results do not appear to work this way. Is there any

Why SQL Server Ignores vaules in string concatenation when ORDER BY clause specified

南楼画角 提交于 2019-11-29 12:08:01
I have the following table Created Comment 2010/10/10 Text1 2010/11/11 Text2 2010/12/12 Text3 I need gather all comments into the single string SELECT @Comment = COALESCE(@Comment, '') + CHAR(13) + CHAR(10) + CONVERT(NVARCHAR(30), [dbo].[Comment].[Created], 101) + ': ' + ISNULL([Comment].[Text], '') FROM Comment Without ordering it works as expected end return all thee comments. But after running following code where ORDER BY clause is added: SELECT @Comment = COALESCE(@Comment, '') + CHAR(13) + CHAR(10) + CONVERT(NVARCHAR(30), [Created], 101) + ': ' + ISNULL([Text], '') FROM Comment ORDER BY

Alphanumeric Order By in Mysql

隐身守侯 提交于 2019-11-29 11:56:28
How can i make only numeric order by when the column containing alphanumeric characters in mysql ? column (name) is unique field. my table contains the records, id name 1 ab001 2 ab010 3 aa002 4 ac004 5 ba015 6 ba006 7 aa005 8 ac003 The results must be like this, id name 1 ab001 3 aa002 8 ac003 4 ac004 7 aa005 6 ba006 2 ab010 5 ba015 When I am trying this query Select * from test order by name , I am getting the results order by alpha characters only. How do I get this ? Mark Byers Assuming your strings always end with 3 digits you could use RIGHT : SELECT id, name FROM Table1 ORDER BY RIGHT

JPQL ORDER BY clause with parameter

梦想与她 提交于 2019-11-29 11:34:32
问题 I'm trying to write a JPQL Query with an ORDER BY clause: query = "SELECT c FROM item ORDER BY c.name ASC" I would like to set an "order" parameter, whose value would be either "ASC" or "DESC": query = "SELECT c FROM item ORDER BY c.name :order" And then in my implementation: query.setParameter("order", "ASC"); This is when I get an Hibernate error: org.hibernate.HibernateException: Errors in named queries Any idea on what I'm doing wrong? Thanks! 回答1: The "ASC" or "DESC" can't be a query

Is it possible to refer to column names via bind variables in Oracle?

夙愿已清 提交于 2019-11-29 11:25:13
I am trying to refer to a column name to order a query in an application communicating with an Oracle database. I want to use a bind variable so that I can dynamically change what to order the query by. The problem that I am having is that the database seems to be ignoring the order by column. Does anyone know if there is a particular way to refer to a database column via a bind variable or if it is even possible? e.g my query is SELECT * FROM PERSON ORDER BY :1 (where :1 will be bound to PERSON.NAME ) The query is not returning results in alphabetical order, I am worried that the database is

JOIN, GROUP BY, ORDER BY

痴心易碎 提交于 2019-11-29 11:14:43
The problem I first had with the following query was that the group by clause was performed before the order by : The saved.recipe_id column is an integer generated by UNIX_TIMESTAMP() SELECT saved.recipe_id, saved.`date`, user.user_id FROM saved JOIN user ON user.id = saved.user_id GROUP BY saved.recipe_id ORDER BY saved.`date` DESC So I tried all sorts of different possible solution with sub queries and other bs. In the end I ended up with trying out some different sub queries in the join clause witch required me to change the table order from the from clause to the join clause. I decided to