sql-order-by

Extremely slow PostgreSQL query with ORDER and LIMIT clauses

梦想与她 提交于 2019-11-29 10:39:55
问题 I have a table, let's call it "foos", with almost 6 million records in it. I am running the following query: SELECT "foos".* FROM "foos" INNER JOIN "bars" ON "foos".bar_id = "bars".id WHERE (("bars".baz_id = 13266)) ORDER BY "foos"."id" DESC LIMIT 5 OFFSET 0; This query takes a very long time to run (Rails times out while running it). There is an index on all IDs in question. The curious part is, if I remove either the ORDER BY clause or the LIMIT clause, it runs almost instantaneously. I'm

OrderBy in SQL Server to put positive values before negative values

痴心易碎 提交于 2019-11-29 10:35:48
I have table in SQL Server which contains a column of type "int". The column can contain positive as well as negative values. I want to carry out sorting based on this column values such that rows with positive values in this column come before the negative values. Example: Code SortColumn A 1 B 5 C -1 D -3 E 0 F 2 Desired Output: Code SortColumn E 0 A 1 F 2 B 5 C -3 D -1 Select * from Table order by Case when sortcolumn<0 then 1 else 0 end ,sortcolumn 来源: https://stackoverflow.com/questions/14908147/orderby-in-sql-server-to-put-positive-values-before-negative-values

MySQL conditional ORDER BY ASC/DESC for date column

一笑奈何 提交于 2019-11-29 10:30:52
I need a MySQL conditional ORDER BY statement for a datetime field. I have a table with posts which I would like to order in the following way: all future posts should be ordered ASC and all historical posts ordered DESC . Eg.: post_status post_date post_title =========== ========= ========== future 2012-10-01 Title 1 future 2012-12-01 Title 2 publish 2012-05-01 Title 3 publish 2012-01-01 Title 4 I need something similar to the following SQL... SELECT post_status, post_date, post_title FROM wp_posts WHERE post_status IN ('future', 'publish') ORDER BY post_status ASC, CASE post_status WHEN

ORDER BY … COLLATE in SQL Server

对着背影说爱祢 提交于 2019-11-29 10:17:48
Under SQL Server. A table contains some text with different cases. I want to sort them case-sensitive and thought that a COLLATE in the ORDER BY would do it. It doesn't. Why? CREATE TABLE T1 (C1 VARCHAR(20)) INSERT INTO T1 (C1) VALUES ('aaa1'), ('AAB2'), ('aba3') SELECT * FROM T1 ORDER BY C1 COLLATE Latin1_General_CS_AS SELECT * FROM T1 ORDER BY C1 COLLATE Latin1_General_CI_AS Both queries return the same, even if the first one is "CS" for case-sensitive aaa1 AAB2 aba3 (in the first case, I want AAB2, aaa1, aba3 ) My server is a SQL Server Express 2008 (10.0.5500) and its default server

Mysql slow query: INNER JOIN + ORDER BY causes filesort

丶灬走出姿态 提交于 2019-11-29 10:01:59
I'm trying to optimize this query: SELECT `posts`.* FROM `posts` INNER JOIN `posts_tags` ON `posts`.id = `posts_tags`.post_id WHERE (((`posts_tags`.tag_id = 1))) ORDER BY posts.created_at DESC; The size of tables is 38k rows, and 31k and mysql uses "filesort" so it gets pretty slow. I tried to use different indexes, no luck. CREATE TABLE `posts` ( `id` int(11) NOT NULL auto_increment, `created_at` datetime default NULL, PRIMARY KEY (`id`), KEY `index_posts_on_created_at` (`created_at`), KEY `for_tags` (`trashed`,`published`,`clan_private`,`created_at`) ) ENGINE=InnoDB AUTO_INCREMENT=44390

How can I sort by the id of a ManyToManyField in Django?

早过忘川 提交于 2019-11-29 09:54:15
I've got a ManyToManyField in a user object and it's used to map the users that user is following. I'm trying to show a subset list of who they have most recently followed. Is there a trick in .order_by() that will allow me to sort by the id of the ManyToManyField? The data is there, right? # (people the user is following) following = models.ManyToManyField(User, related_name="following", blank=True) theuser.following.filter(user__is_active=True).order_by("user__id") That will give me a list of the users the user is following but ordered by when they joined. I want the order of the following

How to select mysql rows in the order of IN clause

核能气质少年 提交于 2019-11-29 08:58:55
问题 For example I have in the table EMPLOYEE: (code, name) (1, 'Jimmy') (2, 'Albert') (3, 'Michelle') (4, 'Felix' ) if you do: (select * from EMPLOYEE) you will get: (1, 'Jimmy') (2, 'Albert') (3, 'Michelle') (4, 'Felix' ) if you do: (select * from EMPLOYEE where code in (1,3,2,4) you will get: (1, 'Jimmy') (2, 'Albert') (3, 'Michelle') (4, 'Felix' ) How to get it in the order of CSV values in the IN clause, as is? (1, 'Jimmy') (3, 'Michelle') (2, 'Albert') (4, 'Felix' ) 回答1: Use the FIND_IN_SET

Find TOP 10 latest record for each BUYER_ID for yesterday's date

…衆ロ難τιáo~ 提交于 2019-11-29 08:27:51
This is the below table CREATE TABLE IF NOT EXISTS TestingTable1 ( BUYER_ID BIGINT, ITEM_ID BIGINT, CREATED_TIME STRING ) And this is the below data in the above table- BUYER_ID | ITEM_ID | CREATED_TIME ------------+------------------+----------------------- 1015826235 220003038067 2012-07-09 19:40:21, 1015826235 300003861266 2012-07-09 18:19:59, 1015826235 140002997245 2012-07-09 09:23:17, 1015826235 210002448035 2012-07-09 22:21:11, 1015826235 260003553381 2012-07-09 07:09:56, 1015826235 260003553382 2012-07-09 19:40:39, 1015826235 260003553383 2012-07-09 06:58:47, 1015826235 260003553384

How to dynamically order by certain entity properties in Entity Framework 7 (Core)

烂漫一生 提交于 2019-11-29 07:35:22
I have a project where the front-end JavaScript specifies a list of columns to order by. Then in the back-end I have multi-layer application. Typical scenario Service layer (the service models' (DTO) properties match whatever the client-side wants to order by) Domain layer (it exposes repository interfaces to access persisted objects) ORM layer (it implements the repository and it uses Entity Framework 7 (a.k.a Entity Framework Core) to access a SQL Server database) Please note that System.Linq.Dynamic IS NOT supported for DNX Core v5.0 or .NET Platform v5.4 so I cannot use that library. I

SQL: How to keep rows order with DISTINCT?

China☆狼群 提交于 2019-11-29 07:34:27
The following SQL query: SELECT messages.id, messages.created_at, comments.created_at FROM messages LEFT JOIN comments ON comments.message_id = messages.id WHERE (messages.id IN (429,443)) ORDER BY GREATEST(messages.created_at, comments.created_at) DESC returns: id messages.created_at comments.created_at -------------------------------------------------------- 443 2 5 429 1 4 443 2 3 (I replaced dates with numbers for readability) To get each id only once I added DISTINCT : SELECT DISTINCT messages.id FROM messages LEFT JOIN comments ON comments.message_id = messages.id WHERE (messages.id IN