sql-order-by

How to show rows in packs of three in MySQL

白昼怎懂夜的黑 提交于 2019-12-11 05:34:51
问题 I have a table like this: ID Type Timestamp 1 A 101 2 A 102 3 B 103 4 B 104 5 A 105 6 B 106 7 A 107 8 A 108 9 B 109 10 A 110 11 B 111 12 B 112 ... I want to show a result sorted by Type and Timestamp where every 3 rows the Type changes like this: ID Type Timestamp 1 A 101 2 A 102 5 A 105 3 B 103 4 B 104 6 B 106 7 A 107 8 A 108 10 A 110 9 B 109 11 B 111 12 B 112 ... 回答1: If you are running MySQL 8.0, consider: SELECT * FROM mytable ORDER BY FLOOR((ROW_NUMBER() OVER(PARTITION BY type ORDER BY

how to do order-by in hibernate mapping

。_饼干妹妹 提交于 2019-12-11 04:56:17
问题 suppose Object A has a list of Object B, and Object B must have a object C, B can be order base on C.level . In A.hbm.xml <bag name="listB" table="T_B" inverse="false" order-by="?? what should i do here???" > <key column="ID_A" not-null="true"/> <many-to-many column="ID_B" class="B"/> </bag> 回答1: The order-by clause should contain the SQL snippet you would use to order the list of items. If your ordering criteria is not located in the table T_B in your example, then you probably won't be able

Order table by grouping values in another table

不打扰是莪最后的温柔 提交于 2019-12-11 04:55:30
问题 I have a table of questions: title Each question has answers, in another table: text, question_id Each answer has votes, in another table: answer_id I can ask for total votes with question.votes.count I have seen how to group the votes db at http://guides.rubyonrails.org/active_record_querying.html#group Vote.group("answer_id") But I want to order my questions by votes. That is ordering an array from a table via the grouping of another table. Is that possible? q=Question.last q.answers.order

When I ORDER BY a computed column, the query slows significantly - Can this be sped up?

痞子三分冷 提交于 2019-12-11 04:28:13
问题 The following query is taking more than 25 seconds to complete: SELECT c.* , (c.age+ (UNIX_TIMESTAMP()-UNIX_TIMESTAMP(c.newdate))) AS ranking , IF(uc.id_user = 7,1,0) AS favorite FROM c LEFT OUTER JOIN uc ON uc.id_cluster = c.id AND uc.id_user = '7' LEFT OUTER JOIN d ON d.id_cluster = c.id LEFT OUTER JOIN dt0 ON dt0.id_document = d.id LEFT OUTER JOIN t0 ON dt0.id_term = t0.id WHERE MATCH(t0.normalizacion) AGAINST ('term' IN NATURAL LANGUAGE MODE) GROUP BY c.id ORDER BY ranking ASC LIMIT 30

How to make a MySQL query to return the ordered records on a “joins association” attribute/column?

为君一笑 提交于 2019-12-11 04:25:25
问题 I have models roughly as follows: class Article < ApplicationRecord has_many :writing_associations has_many :comments, :through => :writing_associations has_many :author_article_associations has_many :authors, :through => author_article_associations end class Comment < ApplicationRecord has_many :writing_associations has_many :articles, :through => :writing_associations has_many :author_comment_associations has_many :authors, :through => :author_comment_associations def self.articles

How to order or choose rows in MySQL GROUP BY clause?

Deadly 提交于 2019-12-11 03:59:18
问题 I have a table like this: id number otherfields ------------------------------------------- 664 48 aaa 665 49 bbb 666 55 ccc 667 48 ddd My query groups by the number field, and I want it to pick the first (lowest) id , so that the data comes out like ccc,aaa,bbb (when ordered by number ). However I'm getting ccc,ddd,bbb - in other words, it's picking row #667 instead of #664 for the number 48. Oddly, this only happens on the live server; on localhost I get it the correct way even though the

How to sort strings as numbers in SQL?

不羁岁月 提交于 2019-12-11 03:54:14
问题 Can anyone tell me how to fix this? My order by is by coursenum (for example, CS 20, CS 25, CS 100 are all course numbers) ascending. It's counting the first digit instead of the whole number though: ----------------------------------------- Course Grade ----------------------------------------- CS 120 Intro to Java Programming A CS 140 Structured Analysis A CS 20 Intro to Computers F CS 25 Intro to Programming B 回答1: The problem here is that your database is doing a string sort, rather than

issue in order clause and limit in active record

柔情痞子 提交于 2019-12-11 03:46:59
问题 I am triggering this query p "my query starts here..." @relevant_customers_for_total_spent = Customer.order("total_amount DESC").where('id IN (?)',@customers_ids).limit(relevant_customers_count_for_total_spent) p " ------- " p relevant_customers_count_for_total_spent # output is: 1139 p @relevant_customers_for_total_spent.count # output is: 2888 p " ------- " More over log is saying the actual query fired is: SELECT COUNT(*) FROM `customers` WHERE (id IN (2,3,4,5,6,75,56,57,58,59,60,61,62,63

SQL SELECT ORDER BY multiple columns depending on value of other column

大兔子大兔子 提交于 2019-12-11 03:32:47
问题 I have a table with the following columns: id | revisit (bool) | FL (decimal) | FR (decimal) | RL (decimal) | RR (decimal) | date I need to write a SELECT statement that will ORDER BY on multiple columns, depending on the value of the 'revisit' field. ORDER BY 'revisit' DESC - records with this field having the value 1 will be first, and 0 will be after If 'revisit' = 1 order by the lowest value that exists in FL, FR, RL and RR. So if record 1 has values 4.6, 4.6, 3.0, 5.0 in these fields,

PostgreSQL CURSOR with “order by” clause

女生的网名这么多〃 提交于 2019-12-11 02:47:28
问题 Let's suppose there is a Query called A and it takes 2sec. SELECT ... FROM ... ORDER BY "users_device"."id" # Query A # It contains join clause. # It takes 2sec However, When I run A with declaring CURSOR , it takes 8sec. DECLARE "cursor" NO SCROLL CURSOR WITH HOLD FOR SELECT ... FROM ... ORDER BY "users_device"."id" # It takes 8sec I have tried to compare Query Plan between them and then I found A with CURSOR seems to try to avoid sorting operation. The below is actual query plan. # A