sql-order-by

Selecting the best score per player

Deadly 提交于 2019-12-14 02:57:36
问题 I have a table called Scores which contains columns: id , player_id , value1 , value2 , value3 and date . The table has next following content: +------+-----------+--------+--------+--------+------------+ | id | player_id | value1 | value2 | value3 | date | +------+-----------+--------+--------+--------+------------+ | 1 | 1 | 10 | 0 | 0 | 2012-08-02 | +------+-----------+--------+--------+--------+------------+ | 2 | 2 | 15 | 1 | 0 | 2012-08-03 | +------+-----------+--------+--------+-------

mysql row number implementation that is always incremental (sorted from 1 to n)

二次信任 提交于 2019-12-14 02:16:54
问题 My previous search results (containing MySQL - row number in recordset? that really helped me) was not satisfying because sorting by other columns overrides the native sort of @RowNumber . Consider my simplified member table below: row_number | id | firstname | lastname -------------------------------------- 1 | 1 | Steve | Jobs 2 | 2 | Bill | Gates 3 | 3 | Rasmus | Lerdorf 4 | 4 | Linus | Torvalds resulted by the following query: SELECT id, firstname, lastname, @RowNumber = @RowNumber + 1 AS

How to assign rank to students to where they share the highest rank when their scores equal?

若如初见. 提交于 2019-12-14 00:14:20
问题 I have code that ranks students and it works, but not as I intended -- if two or more students score the same score it doesn't rank them both in the same rank . I want the final ranking to be like this - if two or more students score the same result, I want them to rank in the same place like this: 1. miki ==> 97.8 2. lisa ==> 96.1 2. jack ==> 96.1 4. john ==> 90.7 Note that lisa and jack score the same ( 96.1 ), and it gives them the same rank (2nd), and therefore, 3rd is skipped. John is

SELECT TOP with multiple UNION and with ORDER BY

青春壹個敷衍的年華 提交于 2019-12-13 21:47:31
问题 I want to select the latest records from the DB in SQL Server. If only one item is selected the final output is this: SELECT TOP(10) * from dbo.Eventos WHERE Tipo LIKE '%OS%' AND Distrito LIKE '%' + always added at the end: ORDER BY Data DESC NOTE : Distrito LIKE '%' must stay as it sometimes is programatically changed to something other than % . If there are more items selected, the query gets one UNION line added programatically for each item . At the end, the ORDER BY is added as always.

SQLite ORDER BY performance issue

落花浮王杯 提交于 2019-12-13 21:18:53
问题 I have a query with two joins (connecting three tables) that runs fairly fast (usually less than 1ms). If I sort the results based on an indexed column of any of the three tables, it still runs in less than 1 ms. However, when I use a combination of two columns (from two different tables) for sorting, the running time is around 240ms. Do I need something like a composite index for columns from different tables? I assume this is not possible. Do I have to use indexed views to achieve a similar

Oracle SQL Grouping/Ordering

喜你入骨 提交于 2019-12-13 21:09:58
问题 I'm looking for some help in writing an Oracle SQL statement to accomplish the following ... Let's say I have the following data: YEAR | PLACE 1984 | somewhere 1983 | work 1985 | somewhere 1982 | home 1984 | work 1983 | home 1984 | somewhere How can I get a result that keeps all the PLACE column values together and orders it by the YEAR column ... so the result I'm looking for is: YEAR | PLACE 1982 | home 1983 | home 1983 | work 1984 | work 1984 | somewhere 1984 | somewhere 1985 | somewhere

How to sort the result of multiple queries alternatively?

与世无争的帅哥 提交于 2019-12-13 19:46:24
问题 I have a query made of three select clause like this: select id, colors from table1 union all select id, numbers from table2 union all select id, names from table3 Also here is the tables structure: // table1 // table2 //table3 +----+--------+ +----+---------+ +----+-------+ | id | colors | | id | numbers | | id | names | +----+--------+ +----+---------+ +----+-------+ | 1 | red | | 1 | ten | | 1 | jack | | 2 | green | | 2 | two | | 2 | peter | | 3 | blue | | 3 | one | +----+-------+ | 4 |

Order IEnumerable by occurrences

纵饮孤独 提交于 2019-12-13 19:14:50
问题 I have an enumerable collection of entities which have come from Linq2Sql (they have been enumerated to an array by this stage). The collection may (probably will) contain multiple occurrences of the same entity. How would I go about ordering the collection so that entities which occurred most often are moved to the front? IEnumerable<Entity> results = SearchForEntities(searchCriteria); return results.OrderByDescending(e => /* Number of occurences in results? */) .Distinct() .Take

Does sql server minds the way records where inserted?

点点圈 提交于 2019-12-13 16:28:20
问题 (a "How it works" - question) Suppose I have this table : Id val ------------------ 1 a <-- 1 a 1 a 2 b <-- 2 b 2 b Now lets say I want the marked rows : Can I count on this query : select id,val from ( select id , val , row_number() over (partition by id order by id) as rn ) k where rn=1 to give me the Selected rows ? (notice the order by clause). will it consider the order as the order they were inserted ? 回答1: The behaviour is not guaranteed. Your query will return two rows, but which ones

Multi Join in a single SQL query

允我心安 提交于 2019-12-13 15:40:23
问题 Below is the data in TestingTable1 sorted by date in descending order always BUYER_ID | ITEM_ID | CREATED_TIME ----------+-----------------+---------------------- 1345653 110909316904 2012-07-09 21:29:06 1345653 151851771618 2012-07-09 19:57:33 1345653 221065796761 2012-07-09 19:31:48 1345653 400307563710 2012-07-09 18:57:33 And if this is the below data in TestingTable2 sorted by date in descending order always USER_ID | PRODUCT_ID | LAST_TIME ---------+----------------+---------------------