sql-order-by

SQL: Select latest thread and latest post, grouped by forum, ordered by latest post

二次信任 提交于 2019-12-07 14:18:34
问题 I'm trying to get the latest thread (id, topic, timestamp, author_id) and latest post (id, thread_id, timestamp, author_id) of each forum (id, name) ordered by the latest post , indepent from the thread's creationdate. Why? I'd like to be able to display details like: "The latest Answer of forum $forum_id was given on Question $thread_id. Here it is: $post_id" SELECT f.id AS forum_id, f.name AS forum_name, t.id AS thread_id, t.topic AS thread_topic, t.ts AS thread_timestamp, p.id AS post_id,

order by within group concat

十年热恋 提交于 2019-12-07 11:28:31
问题 The order by is not working in the second query. I need to order by first DNAID then DNBID First Query its ordered as: 111221 Second Query its ordered as: 112112 for more info and details about what im trying to accomplish https://stackoverflow.com/questions/5082880/database-query-group-union-find-latest mysql> select * from metarun; +----+------------+-------+-------+--------------+----------+ | ID | RunGroupID | DNAID | DNBID | CONFIGTYPEID | DateTime | +----+------------+-------+-------+--

Selecting the first row from each group, with ORDER BY more than one column

元气小坏坏 提交于 2019-12-07 10:48:46
问题 I have a table T with columns x , y , a , b such that SELECT x,y,a,b FROM T ORDER BY x,y,a,b gives me the following table x | y | a | b x1 | y1 | a1 | b1 x1 | y1 | a1 | b2 x1 | y1 | a2 | b1 x1 | y2 | a1 | b1 x1 | y2 | a1 | b2 x1 | y2 | a2 | b1 How would I get the first row of each x,y group? That is, how would I get the following table x | y | a | b x1 | y1 | a1 | b1 x1 | y2 | a1 | b1 Here is a second example: For a table T such that x | y | a | b x1 | y1 | a1 | b3 x1 | y1 | a1 | b4 x1 | y1 |

“Invalid column name”, Order By on Alias of subquery

一个人想着一个人 提交于 2019-12-07 10:20:18
问题 I've created a stored procedure where i want to add an alternative order by clause. The problen is that the query failed on a "Invalid column name 'aantal regels'" Here is the query I have now. SELECT l.lead_id, l.afdeling_id, l.advertentie_id, l.naam, l.type, l.status, l.herkomst, l.aanmaakdatum, l.klant_id, l.overigegegevens, af.afdelingsnaam, (SELECT COUNT(lead_regel_id) FROM Lead_regel As lr Where Lr.lead_id = l.lead_id And lr.status <> 100 ) AS aantal_regels, (SELECT COUNT(lead_id) FROM

Order resultset based on WHERE IN clause data

假装没事ソ 提交于 2019-12-07 10:08:31
问题 Considering this MySQL query: SELECT someColumns FROM someTable WHERE someColumn IN ( value1, value2, value3 ) ... how can I guarantee that the rowset comes out ordered in the exact order of the values given to the IN () clause? I presume this is not guaranteed without giving it an ORDER BY clause, is it? PS.: The values to the IN () clause will be an array of arbitrary data passed to the query by PHP (utilizing Zend Framework's select statement) in the following manner: ->where( 'someColumn

Making sense of 'OFFSET/FETCH' in SSMS 2012

ぐ巨炮叔叔 提交于 2019-12-07 09:52:29
Just installed Microsoft SQL Server Management Studio 2012 today. In familiarizing myself with the pagination feature addition of ORDER BY, I keep running into this error: Msg 102, Level 15, State 1, Line 5 Incorrect syntax near 'OFFSET'. Msg 153, Level 15, State 2, Line 6 Invalid usage of the option NEXT in the FETCH statement. Here is my query: SELECT SingleWomansName, NumberOfCats FROM CatLadies WHERE NumberOfCats > 1 ORDER BY NumberOfCats OFFSET 10 ROWS FETCH NEXT 5 ROWS ONLY I've seen plenty of how-to articles with similar syntax. What gives? http://msdn.microsoft.com/en-us/library

Can not ORDER BY an AVG value with certain GROUP BY criteria in MySQL

☆樱花仙子☆ 提交于 2019-12-07 07:37:08
问题 I have a table data_summaries . It has such columns as item_id INT(11) , user_grouping TEXT and value DECIMAL(10,2) . If I try to make a query that groups the results by user_grouping and orders them by the AVG of value , that fails: SELECT user_grouping, AVG(value) AS avg_value, SUM(value) AS sum_value FROM data_summaries GROUP BY user_grouping ORDER BY avg_value +---------------+-----------+-----------+ | user_grouping | avg_value | sum_value | +---------------+-----------+-----------+ |

MySQL Order from a starting value

佐手、 提交于 2019-12-07 07:07:09
问题 On a product page I have a dropdown that lists current colour options associated to product page. In this example, the product page SKU is 250E and it available in: GREEN BLACK If the customer selects GREEN, then I want to run a MySQL command that will change the data to show GREEN values first based in the custom_order value shown below. The start value should overide the other data items and then it should retain custom_order values. The custom_order field has letters like c1, c2 (they will

MySQL group by with ordering/priority of another column

青春壹個敷衍的年華 提交于 2019-12-07 06:05:17
问题 I've already looked at these two questions: Grouping by Column with Dependence on another Column MySQL GROUP BY with preference However both of them use an aggregate function MAX in order to obtain the highest or filled in value, which doesn't work for my case. For the purposes of this question, I've simplified my situation. Here is my current data: I'd like to obtain the operator name for each route, but with respect to direction of travel (i.e. ordering or "preferring" values). This is my

Using [0] or First() with LINQ OrderBy

岁酱吖の 提交于 2019-12-07 03:43:24
If I have a structure like this Albums - Album - Discs - Tracks and I want to order a collection of albums by the title of the first track on the first disc. Is there something similar to the following I could do (keeping in mind I need to use the OrderBy extension method that accepts a string)? albums.OrderBy("Discs[0].Tracks[0].Title") I need to be able to sort using a string expression thus the need to use the OrderBy method i.e. albums.OrderBy("Track[0].Title"). The reason for this is our custom framework uses a sort expression (e.g. "Title") passed back from a GridView which is looked up