sql-order-by

MySQL select top rows with same condition values

喜你入骨 提交于 2019-12-30 06:27:09
问题 I don't know how to title this problem. Correct me if you have better words. I have two tables, Users and Posts. Users: id | username | password | ... Posts: id | author_id | title | content | ... Now I want to list the "most active" users - the users who have written the most posts. And specifically, I want the top 10 result. SELECT u.username, COUNT(p.id) AS count FROM Posts p, Users u WHERE u.id=p.author_id GROUP BY p.author_id ORDER BY count DESC LIMIT 10; I can get the expected result.

What can an aggregate function do in the ORDER BY clause?

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-30 03:03:33
问题 Lets say I have a plant table: id fruit 1 banana 2 apple 3 orange I can do these SELECT * FROM plant ORDER BY id; SELECT * FROM plant ORDER BY fruit DESC; which does the obvious thing. But I was bitten by this, what does this do? SELECT * FROM plant ORDER BY SUM(id); SELECT * FROM plant ORDER BY COUNT(fruit); SELECT * FROM plant ORDER BY COUNT(*); SELECT * FROM plant ORDER BY SUM(1) DESC; All these return just the first row (which is with id = 1). What's happening underhood? What are the

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

假装没事ソ 提交于 2019-12-29 08:09:08
问题 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

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

筅森魡賤 提交于 2019-12-29 08:09:04
问题 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

Slow query ordering by a column in a joined table

大城市里の小女人 提交于 2019-12-29 06:47:55
问题 Introducing an ORDER BY clause in a query increases the total time due the extra work that the db have to do in order to sort the result set: copy the resulting tuples in some temporary memory sorting them (hopefully in memory, otherwise using the disk) stream the result to the client What I miss is why just adding a column from a joined table produces a so different performance. Query1 EXPLAIN ANALYZE SELECT p.* FROM product_product p JOIN django_site d ON (p.site_id = d.id) WHERE (p.active

MySQL order by string with numbers

南楼画角 提交于 2019-12-28 20:37:16
问题 I have strings such as M1 M3 M4 M14 M30 M40 etc (really any int 2-3 digits after a letter) When I do " ORDER BY name " this returns: M1, M14, M3, M30, M4, M40 When I want: M1, M3, M4, M14, M30, M40 Its treating the whole thing as a string but I want to treat it as string + int Any ideas? 回答1: You could use SUBSTR and CAST AS UNSIGNED/SIGNED within ORDER BY: SELECT * FROM table_name ORDER BY SUBSTR(col_name FROM 1 FOR 1), CAST(SUBSTR(col_name FROM 2) AS UNSIGNED) 回答2: If there can be multiple

MySQL order by string with numbers

﹥>﹥吖頭↗ 提交于 2019-12-28 20:36:32
问题 I have strings such as M1 M3 M4 M14 M30 M40 etc (really any int 2-3 digits after a letter) When I do " ORDER BY name " this returns: M1, M14, M3, M30, M4, M40 When I want: M1, M3, M4, M14, M30, M40 Its treating the whole thing as a string but I want to treat it as string + int Any ideas? 回答1: You could use SUBSTR and CAST AS UNSIGNED/SIGNED within ORDER BY: SELECT * FROM table_name ORDER BY SUBSTR(col_name FROM 1 FOR 1), CAST(SUBSTR(col_name FROM 2) AS UNSIGNED) 回答2: If there can be multiple

java comparator, how to sort by integer?

落爺英雄遲暮 提交于 2019-12-28 11:42:53
问题 Im trying to learn comparator in java and I have found this great example online, my question is how would this code be changed so that the pet names are ordered by age and in descending order so that the oldest is first and youngest is last? class Dog implements Comparator<Dog>, Comparable<Dog>{ private String name; private int age; Dog(){ } Dog(String n, int a){ name = n; age = a; } public String getDogName(){ return name; } public int getDogAge(){ return age; } // Overriding the compareTo

How does PostgreSQL perform ORDER BY if a b-tree index is built on that field?

守給你的承諾、 提交于 2019-12-28 04:29:12
问题 I have a table bsort : CREATE TABLE bsort(a int, data text); Here data may be incomplete. In other words, some tuples may not have data value. And then I build a b-tree index on the table: CREATE INDEX ON bsort USING BTREE(a); Now if I perform this query: SELECT * FROM bsort ORDER BY a; Does PostgreSQL sort tuples with nlogn complexity, or does it get the order directly from the b-tree index? 回答1: For a simple query like this Postgres will use an index scan and retrieve readily sorted tuples

SQL Error with Order By in Subquery

十年热恋 提交于 2019-12-28 03:29:51
问题 I'm working with SQL Server 2005. My query is: SELECT ( SELECT COUNT(1) FROM Seanslar WHERE MONTH(tarihi) = 4 GROUP BY refKlinik_id ORDER BY refKlinik_id ) as dorduncuay And the error: The ORDER BY clause is invalid in views, inline functions, derived tables, subqueries, and common table expressions, unless TOP or FOR XML is also specified. How can I use ORDER BY in a sub query? 回答1: This is the error you get (emphasis mine): The ORDER BY clause is invalid in views, inline functions, derived