sql-order-by

MySQL Rating With Weight

夙愿已清 提交于 2019-12-06 11:28:21
I want to create a rating with weight depending on number of votes. So, 1 voting with 5 can't be better than 4 votings with 4. I found this math form: bayesian = ( (avg_num_votes * avg_rating) + (this_num_votes * this_rating) ) / (avg_num_votes + this_num_votes) How can I make a MySQL SELECT to get the ID of the best rating image. I got a table for IMAGE, and a table for VOTING VOTING: id imageID totalVotes avgVote I think I got to do this with SELECT in SELECT, but how? A first step is to calculate avg_num_votes and avg_rating : SELECT SUM(totalVotes)/COUNT(*) AS avg_num_votes, SUM(avgVote)

How to optimize MySQL “Order By Limit 1” in queries that join multiple tables?

坚强是说给别人听的谎言 提交于 2019-12-06 10:24:01
So I have a query like this: SELECT tablea.name, tablea.views from tablea inner join tableb on (tablea.id = tableb.id and tablea.balance > 0) order by tablea.views asc limit 1 However, the problem is that when I run it, it runs quite slow (4+ seconds). Interestingly, when the 'order by' clause is removed, while keeping the limit 1, it runs in 0.005 seconds (approx). Even more interestingly: when I don't join it to tableb, i.e.: SELECT tablea.name, tablea.views from tablea where tablea.balance > 0 order by tablea.views asc limit 1 The query runs in 0.005 seconds usually. Notes: The column views

ORDER BY for currency values

左心房为你撑大大i 提交于 2019-12-06 09:37:08
I have a prize_value column in one of the MySQL tables and now I need to do sorting based on this. This column is actually VARCHAR and has currency symbols attached with it. But not for all the values. The currency symbol can be USD(dollar sign), POUND or INR (Rupee Symbol). So currently the order by is not working properly. How can I fix this without removing the currency symbol manually ? Here are some sample values in the column: .50 £10 £100 $15 $20 £25 £50 10 ₹30 You need two columns: one for the value, a float/double (or an integer) that can be ordered, used in operations such as sums,

MySQL GROUP BY / ORDER BY issue with flat messages table / threads

白昼怎懂夜的黑 提交于 2019-12-06 08:10:38
Ok, I'm trying to base something similar off of this, but not quite getting it nailed: GROUP BY and ORDER BY Basically, wanting a query to find the latest messages in each 'thread' between the current logged-in user and any other users, but via a flat (non-'threaded') table of messages: messages { id, from_uid, to_uid, message_text, time_added } Assuming the current user's uid is '1', and the latest message in each 'thread' could either be from that user, or to that user (the other party always denoted by thread_recipient): SELECT a.*,thread_recipient FROM messages a JOIN (SELECT IF(from_uid =

How to sort a varchar datatype column by its numbers in SQL Server

▼魔方 西西 提交于 2019-12-06 07:50:58
I have a column in SQL Server that contains data like this: 1.1.1.QuestionText 1.1.1.Question 1.1.1(a).Questions 1.1.2.Questionswithtext 1.1.2(b).Text 10.1.1.Answers 2.1.1.Questions 2.2.2.QuestionText How do I display this in ascending order? This should work: SELECT name FROM ( SELECT name, SUBSTRING(name, 1, LEN(name) - PATINDEX('%[0-9]%', REVERSE(name)) + 1) n FROM #tmp ) v ORDER BY CAST(PARSENAME(n, 4) AS INT), CAST(PARSENAME(n, 3) AS INT), CAST(PARSENAME(n, 2) AS INT), CAST(PARSENAME(n, 1) AS INT) Output: name 1.1.1.QuestionText 1.1.1.Question 1.1.1(a).Questions 1.1.2.Questionswithtext 1

Postgres ORDER BY values in IN list using Rails Active Record

我只是一个虾纸丫 提交于 2019-12-06 06:52:59
I receive a list of UserIds(about 1000 at a time) sorted by 'Income'. I have User records in "my system's database" but the 'Income' column is not there. I want to retrieve the Users from "my system's database" in the Sorted Order as received in the list. I tried doing the following using Active Record expecting that the records would be retrieved in the same order as in the Sorted List but it does not work. //PSEUDO CODE User.all(:conditions => {:id => [SORTED LIST]}) I found an answer to a similar question at the link below, but am not sure how to implement the suggested solution using

LINQ: Order By Anonymous Type

谁都会走 提交于 2019-12-06 06:10:48
Hello I am using linq to fill a gridview with the information from an xml from codebehind. I would like to order my Grid according to one of my elements in the xml ("value element") but can't figure out how to do this. Any ideas? gvResourceEditor.DataSource = (From resElem In resourceElements.Elements("data") _ Select New With { _ .Key = resElem.Attribute("name").Value, _ .Value = HttpUtility.HtmlEncode(resElem.Element("value").Value), _ .Comment = If(resElem.Element("comment") IsNot Nothing, HttpUtility.HtmlEncode(resElem.Element("comment").Value), String.Empty) _ }).OrderBy(?????)

MySQL query to show records with current date on top and others as per descending order

为君一笑 提交于 2019-12-06 05:57:43
问题 I am using the following query in my database, SELECT b.sales_id,b.category_id,b.sale_starts,b.sale_ends FROM tbl_sales b WHERE b.active=1 UNION SELECT b.sales_id,b.category_id,b.sale_starts,b.sale_ends FROM tbl_sales b INNER JOIN tb_category c ON b.category_id=c.cat_id WHERE c.cat_keyword LIKE 'a' ORDER BY sale_ends DESC and getting the result as follows, sales_id | category_id |sale_starts | sale_ends ----------|---------------------|------------|-------------- 1 | 10 | 2012-03-31 | 2012-04

JPA 2.0 subselect / subquery in order by clause with criteria api

て烟熏妆下的殇ゞ 提交于 2019-12-06 05:51:47
问题 I want to use JPA 2.0 criteria api to build the order by clause with a subselect. I know that you can do that in plain SQL but can it be mapped with criteria api? Can someone please give a code example? Example: Order(name, address) // table1 OrderPriority(address, priority) // table2 priority by address select o from Order o order by (select p.priority from OrderPriority p where p.address = o.address) 回答1: Criteria API queries are converted to JPQL and apparently subqueries in the order by

SQL order by and left outer join doesn't have correct order

一笑奈何 提交于 2019-12-06 05:50:15
I have a view that is joining two tables and ordering by the first table. Except that the order isn't correct. It misses an occasional record, and then at the end, most of those records exist in order, and then at that end, the rest of the records exist in order. So it has records such as 1 (most of the records in order) 2 4 5 6 7 8 10 11 13 15 3 (the first set of missing records) 12 9 (the rest of the missing records) 14 My view is below. Do I need to do the order by before I do the join? What am I doing wrong? (I've acquired this view, and the exact same view in another db instance works