sql-order-by

Postgres CASE in ORDER BY using an alias

↘锁芯ラ 提交于 2019-11-30 06:41:55
I have the following query which works great in Postgres 9.1: SELECT users.id, GREATEST( COALESCE(MAX(messages.created_at), '2012-07-25 16:05:41.870117'), COALESCE(MAX(phone_calls.created_at), '2012-07-25 16:05:41.870117') ) AS latest_interaction FROM users LEFT JOIN messages ON users.id = messages.user_id LEFT JOIN phone_calls ON users.id = phone_calls.user_id GROUP BY users.id ORDER BY latest_interaction DESC LIMIT 5; But what I want to do is something like this: SELECT users.id, GREATEST( COALESCE(MAX(messages.created_at), '2012-07-25 16:05:41.870117'), COALESCE(MAX(phone_calls.created_at),

PostgreSQL - repeating rows from LIMIT OFFSET

跟風遠走 提交于 2019-11-30 06:23:05
I noticed some repeating rows in a paginated recordset. When I run this query: SELECT "students".* FROM "students" ORDER BY "students"."status" asc LIMIT 3 OFFSET 0 I get: | id | name | status | | 1 | foo | active | | 12 | alice | active | | 4 | bob | active | Next query: SELECT "students".* FROM "students" ORDER BY "students"."status" asc LIMIT 3 OFFSET 3 I get: | id | name | status | | 1 | foo | active | | 6 | cindy | active | | 2 | dylan | active | Why does "foo" appear in both queries? Why does "foo" appear in both queries? Because all rows that are returned have the same value for the

PostgreSQL - order by an array

那年仲夏 提交于 2019-11-30 05:59:51
问题 I have 2 tables - course that contains id and name of the courses and tagCourse that contains tags for each course. course tagcourse ------------ ---------------- PK id_course PK tag name PK, FK id_course I'd like to write a function that searches courses by given array of tags and returns them ordered by quantity of matching tags. However I don't know how to write it correctly and in an efficient way. Please help me. ie. CREATE OR REPLACE FUNCTION searchByTags(tags varchar[]) RETURNS SETOF..

How to re-use result for SELECT, WHERE and ORDER BY clauses?

ぐ巨炮叔叔 提交于 2019-11-30 05:59:31
问题 The following query returns the venues near us (lat: 62.0, lon: 25.0) inside whose radius we fall in ordered by distance: SELECT *, earth_distance(ll_to_earth(62.0, 25.0), ll_to_earth(lat, lon)) AS distance FROM venues WHERE earth_distance(ll_to_earth(62.0, 25.0), ll_to_earth(lat, lon)) <= radius ORDER BY earth_distance(ll_to_earth(62.0, 25.0), ll_to_earth(lat, lon)) Is it possible (and advisable) to re-use the result from earth_distance(ll_to_earth(62.0, 25.0), ll_to_earth(lat, lon)) instead

Custom ORDER BY Explanation

时光毁灭记忆、已成空白 提交于 2019-11-30 05:50:21
问题 I found this some time ago and have been using it since; however, looking at it today, I realized that I do not fully understand why it works. Can someone shed some light on it for me? ORDER BY s.type!= 'Nails', s.type!= 'Bolts', s.type!= 'Washers', s.type!= 'Screws', s.type!= 'Staples', s.type!= 'Nuts', ... If I order by s.type, it orders alphabetically. If I use the example above it uses the same order as the line positions. What I don't understand is the use of !=. If I use = it appears in

How to order 1,2,3 not 1, 10, 11, 12 in mySQL

狂风中的少年 提交于 2019-11-30 05:48:11
The following code outputs in order of 1, 10, 11, 12 of id. I want to make it 1,2,3,4... Could anyone tell me what I should do please. $Q = $this->db->query('SELECT P.*, C.Name AS CatName FROM products AS P LEFT JOIN categories C ON C.id = P.category_id'); Thanks in advance. First, add an order by clause at the end: ORDER BY category_id If category_id is a string, then you must treat it like an integer. There are a few ways to do this. I usually add a zero. You can also cast it. ORDER BY category_id + 0 You can do an explicit cast by doing: ORDER BY CAST(category_id AS UNSIGNED INTEGER) But

Incorrect usage of UNION and ORDER BY?

主宰稳场 提交于 2019-11-30 05:34:24
how can i use union and order by in mysql ? select * from _member_facebook inner join _member_pts ON _member_facebook._fb_owner=_member_pts._username where _member_facebook._promote_point = 9 ORDER BY RAND() limit 2 UNION ALL select * from _member_facebook inner join _member_pts ON _member_facebook._fb_owner=_member_pts._username where _member_facebook._promote_point = 8 limit 3 give me error #1221 - Incorrect usage of UNION and ORDER BY any one can help ? Tudor Constantin Try with: ( select * from _member_facebook inner join _member_pts ON _member_facebook._fb_owner=_member_pts._username

How to match and sort by similarity in MySQL?

自古美人都是妖i 提交于 2019-11-30 05:32:43
问题 Currently, I am doing a search function. Lets say in my database, I have this data: Keyword1 Keyword2 Keyword3 Keysomething Key and the user entered: "Key" as the keyword to search. This is my current query: SELECT * FROM data WHERE ( data_string LIKE '$key%' OR data_string LIKE '%$key%' OR data_string LIKE '%$key' ) Basically, I have 2 questions: How do I sort by (order by) similarity. From above example, I wanted "Key" as my first result. My current result is: Keyword1, Keyword2, Keyword3,

Best practice question for MySQL: order by id or date?

强颜欢笑 提交于 2019-11-30 05:06:16
This is kind of a noobish question, but it's one that I've never been given a straight answer on. Suppose I have a DB table with the following fields and values: | id | date_added | balance | +------------------------------------+ | 1 | 2009-12-01 19:43:22 | 1237.50 | | 2 | 2010-01-12 03:19:54 | 473.00 | | 3 | 2010-01-12 03:19:54 | 2131.20 | | 4 | 2010-01-20 11:27:31 | 3238.10 | | 5 | 2010-01-25 22:52:07 | 569.40 | +------------------------------------+ This is for a very basic 'accounting' sub-system. I want to get the most recent balance. The id field is set to auto_increment. Typically, I

How to optimize an ORDER BY for a computed column on a MASSIVE MySQL table

ε祈祈猫儿з 提交于 2019-11-30 05:02:40
I have a very large (80+ million row) de-normalized MySQL table. A simplified schema looks like: +-----------+-------------+--------------+--------------+ | ID | PARAM1 | PARAM2 | PARAM3 | +-----------+-------------+--------------+--------------+ | 1 | .04 | .87 | .78 | +-----------+-------------+--------------+--------------+ | 2 | .12 | .02 | .76 | +-----------+-------------+--------------+--------------+ | 3 | .24 | .92 | .23 | +-----------+-------------+--------------+--------------+ | 4 | .65 | .12 | .01 | +-----------+-------------+--------------+--------------+ | 5 | .98 | .45 | .65 | +