sql-order-by

Update top N values using PostgreSQL

别来无恙 提交于 2019-12-04 00:33:44
I want to update the top 10 values of a column in table. I have three columns; id , account and accountrank . To get the top 10 values I can use the following: SELECT * FROM accountrecords ORDER BY account DESC LIMIT 10; What I would like to do is to set the value in accountrank to be a series of 1 - 10 , based on the magnitude of account . Is this possible to do in PostgreSQL? WITH cte AS ( SELECT id, row_number() OVER (ORDER BY account DESC NULLS LAST) AS rn FROM accountrecords ORDER BY account DESC NULLS LAST LIMIT 10 ) UPDATE accountrecords a SET accountrank = cte.rn FROM cte WHERE cte.id

Order by field in cakephp

 ̄綄美尐妖づ 提交于 2019-12-03 22:57:46
I am doing project in cakephp . I want to write below query in cakephp Style. I've written 50% . Please help me $this->Login->find('all') SELECT * FROM login ORDER BY FIELD(profile_type, 'Basic', 'Premium') DESC; Plese try this $this->Login->find('all', array( 'order'=>array('FIELD(Login.profile_type, "basic", "premium") DESC') )); mensch You can pass options to the find method : $this->Login->find('all', array( 'order' => "FIELD(Login.profile_type, 'Basic', 'Premium') DESC" )); LUIS TOBIO GUTIERREZ Please, try this: $response = $this->Login->find('all', array('order'=>array('Login.profile

Doctrine DQL dynamic ORDER BY parameter

若如初见. 提交于 2019-12-03 22:19:40
问题 Im trying to pass the ORDER BY column as a parameter in DQL, like below: $this->em->createQuery("SELECT t FROM Entities\Topic t ORDER BY :order") ->setParameters( array('order' => 't.name') )->getResult(); I guess it doesn't work because setParameter will escape :order, however the below solution doesn't seem very good: $order = 't.name'; // Dynamic value $this->em->createQuery("SELECT t FROM Entities\Topic t ORDER BY $order") ->getResult(); Is there a better way to solve this? 回答1: In that

SQL: Using Top 1 in UNION query with Order By

喜欢而已 提交于 2019-12-03 20:41:06
问题 I have a table as below Rate Effective_Date ---- -------------- 5.6 02/02/2009 5.8 05/01/2009 5.4 06/01/2009 5.8 12/01/2009 6.0 03/15/2009 I am supposed to find the all rates that are effective for current date and after it. So to get the current effective rate, i use SELECT TOP 1 * from table where effective_date < '05/05/2009' order by effective date desc for the rates after the current date the query is SELECT * from table where effective_date > '05/05/2009' To combine these two result i

Sort by day of the week from Monday to Sunday

亡梦爱人 提交于 2019-12-03 19:46:17
问题 If I write select ename, to_char(hiredate,'fmDay') as "Day" order by "Day"; Then it sorts the result based on Day like; from Friday, then Monday and last Wednesday, like sorting by characters. But I want to sort it by day of the week; from Monday to Sunday. 回答1: You're getting it in the order you are because you're ordering by a string (and this wouldn't work because you're not selecting from anything). You could order by the format model used to create the day of the week in numeric form, D

hibernate order by association

假装没事ソ 提交于 2019-12-03 17:35:42
问题 I'm using Hibernate 3.2, and using criteria to build a query. I'd like to add and "order by" for a many-to-one association, but I don't see how that can be done. The Hibernate query would end up looking like this, I guess: select t1.a, t1.b, t1.c, t2.dd, t2.ee from t1 inner join t2 on t1.a = t2.aa order by t2.dd <-- need to add this I've tried criteria.addOrder("assnName.propertyName") but it doesn't work. I know it can be done for normal properties. Am I missing something? 回答1: Ok, found the

OrderBy(“it.” + sort) — Hard coding in LINQ to Entity framework?

半世苍凉 提交于 2019-12-03 15:28:47
I have been trying to use dynamic LINQ to Entity in my application for specifying the OrderBy attribute at runtime. However when using the code as described in the majority of documentation: var query = context.Customer.OrderBy("Name"); I received the following exception: System.Data.EntitySqlException: 'Name' could not be resolved in the current scope or context. Make sure that all referenced variables are in scope, that required schemas are loaded, and that namespaces are referenced correctly. After much searching I found this MSDN page: http://msdn.microsoft.com/en-us/library/bb358828.aspx

mysql SORT BY amount of unique word matches

人走茶凉 提交于 2019-12-03 14:59:46
I've found many questions that ask for amount of appearences, but none that ask the very same as I wish to do. A dynamically generated (prepared-statement) query will result in something like this: SELECT * FROM products WHERE ( title LIKE ? AND title LIKE ? ) AND ( content LIKE ? OR content LIKE ? ) AND ( subtitle LIKE ? AND author LIKE ? ) ORDER BY relevance LIMIT ?,? The amount of words entered, (and so the amount of LIKE ) are for title,content and author a variable amount (depending on the search query). Now I've added a ORDER BY relevance . But I wish this order to be the amount of

SQL Query: Need order by count, most must be on top, the rest follows

て烟熏妆下的殇ゞ 提交于 2019-12-03 14:12:19
TABLEA JobCode Job1 Job2 Job3 zip ------- ---- ---- ---- ---------- F F S NULL 90030 F F S NULL 90031 F F S NULL 90031 F F S NULL 90034 F F NULL 90034 F F S NULL 90034 F F S NULL 90034 F F NULL 90034 F F S NULL 90035 F F NULL 90035-4640 EXPECTED RESULTS: JobCode Job1 Job2 Job3 zip ------- ---- ---- ---- ---------- F F S NULL 90034 F F NULL 90034 F F S NULL 90034 F F S NULL 90034 F F NULL 90034 F F S NULL 90031 F F S NULL 90031 F F S NULL 90030 F F S NULL 90035 F F NULL 90035-4640 Those with the SAME Zip should be ON top, then the rest follows. ORDER BY Zip does not work because it DOES sort by

Distinct Records with joins and order

南楼画角 提交于 2019-12-03 13:15:25
I have a simple relationship between User and Donations in that a user has many donations, and a donation belongs to a user. What I'd like to do is get a list of users, ordered by the most recent donations. Here's what I'm trying: First I want to get the total number of uniq users, which is working as expected: > User.joins(:donations).order('donations.created_at').uniq.count (3.2ms) SELECT DISTINCT COUNT(DISTINCT "users"."id") FROM "users" INNER JOIN "donations" ON "donations"."user_id" = "users"."id" => 384 Next, when I remove the count method, I get an error that "ORDER BY expressions must