sql-order-by

MySQL - Control which row is returned by a group by

空扰寡人 提交于 2019-11-26 06:06:18
问题 I have a database table like this: id version_id field1 field2 1 1 texta text1 1 2 textb text2 2 1 textc text3 2 2 textd text4 2 3 texte text5 If you didn\'t work it out, it contains a number of versions of a row, and then some text data. I want to query it and return the version with the highest number for each id. (so the second and last rows only in the above). I\'ve tried using group by whilst ordering by version_id DESC - but it seems to order after its grouped, so this doesn\'t work.

Slow query when using ORDER BY

狂风中的少年 提交于 2019-11-26 05:54:27
问题 Here\'s the query (the largest table has about 40,000 rows) SELECT Course.CourseID, Course.Description, UserCourse.UserID, UserCourse.TimeAllowed, UserCourse.CreatedOn, UserCourse.PassedOn, UserCourse.IssuedOn, C.LessonCnt FROM UserCourse INNER JOIN Course USING(CourseID) INNER JOIN ( SELECT CourseID, COUNT(*) AS LessonCnt FROM CourseSection GROUP BY CourseID ) C USING(CourseID) WHERE UserCourse.UserID = 8810 If I run this, it executes very quickly (.05 seconds roughly). It returns 13 rows.

Optimize query with OFFSET on large table

笑着哭i 提交于 2019-11-26 05:36:03
问题 I have table create table big_table ( id serial primary key, -- other columns here vote int ); This table is very big, approximately 70 million rows, I need to query: SELECT * FROM big_table ORDER BY vote [ASC|DESC], id [ASC|DESC] OFFSET x LIMIT n -- I need this for pagination As you may know, when x is a large number, queries like this are very slow. For performance optimization I added indexes: create index vote_order_asc on big_table (vote asc, id asc); and create index vote_order_desc on

When no 'Order by' is specified, what order does a query choose for your record set?

你说的曾经没有我的故事 提交于 2019-11-26 05:33:59
问题 I was always of the impression that a query with no specified \'Order by\' rule, would order this by the results by what was specified within your where clause. For instance, my where clause states: WHERE RESULTS_I_AM_SEARCHING_FOR IN ITEM 1 ITEM 2 ITEM 3 I would have imagined that the results returned for items 1, 2 and 3 would be in the order specified in the where, however this is not the case. Does anyone know what order it sorts them in when not specified? Thanks and sorry for the really

SQL (ORACLE): ORDER BY and LIMIT

。_饼干妹妹 提交于 2019-11-26 05:32:55
问题 I want do sorting by property ALL data in my db and ONLY AFTER that use LIMIT and OFFSET. Query like this: SELECT select_list FROM table_expression [ ORDER BY ... ] [ LIMIT { number | ALL } ] [ OFFSET number ] I know the sorting ends as soon as it has found the first row_count rows of the sorted result. Can I do sorting all data before calling LIMIT and OFFSET? 回答1: Prior to 12.1, Oracle does not support the LIMIT or OFFSET keywords. If you want to retrieve rows N through M of a result set,

GROUP_CONCAT ORDER BY

谁都会走 提交于 2019-11-26 05:27:27
问题 I\'ve a table like: +-----------+-------+------------+ | client_id | views | percentage | +-----------+-------+------------+ | 1 | 6 | 20 | | 1 | 4 | 55 | | 1 | 9 | 56 | | 1 | 2 | 67 | | 1 | 7 | 80 | | 1 | 5 | 66 | | 1 | 3 | 33 | | 1 | 8 | 34 | | 1 | 1 | 52 | I tried group_concat : SELECT li.client_id, group_concat(li.views) AS views, group_concat(li.percentage) FROM li GROUP BY client_id; +-----------+-------------------+-----------------------------+ | client_id | views | group_concat(li

How to use DISTINCT and ORDER BY in same SELECT statement?

牧云@^-^@ 提交于 2019-11-26 05:22:24
问题 After executing the following statement: SELECT Category FROM MonitoringJob ORDER BY CreationDate DESC I am getting the following values from the database: test3 test3 bildung test4 test3 test2 test1 but I want the duplicates removed, like this: bildung test4 test3 test2 test1 I tried to use DISTINCT but it doesn\'t work with ORDER BY in one statement. Please help. Important: I tried it with: SELECT DISTINCT Category FROM MonitoringJob ORDER BY CreationDate DESC it doesn\'t work. Order by

Select the 3 most recent records where the values of one column are distinct

拈花ヽ惹草 提交于 2019-11-26 03:56:19
问题 I have the following table: id time text otheridentifier ------------------------------------------- 1 6 apple 4 2 7 orange 4 3 8 banana 3 4 9 pear 3 5 10 grape 2 What I want to do is select the 3 most recent records (by time desc), whose otheridentifier s are distinct. So in this case, the result would be id \'s: 5, 4, and 2. id = 3 would be skipped because there\'s a more recent record with the same otheridentifier field. Here\'s what I tried to do: SELECT * FROM `table` GROUP BY (

How to use Oracle ORDER BY and ROWNUM correctly?

你。 提交于 2019-11-26 03:52:46
问题 I am having a hard time converting stored procedures from SQL Server to Oracle to have our product compatible with it. I have queries which returns the most recent record of some tables, based on a timestamp : SQL Server: SELECT TOP 1 * FROM RACEWAY_INPUT_LABO ORDER BY t_stamp DESC => That will returns me the most recent record But Oracle: SELECT * FROM raceway_input_labo WHERE rownum <= 1 ORDER BY t_stamp DESC => That will returns me the oldest record (probably depending on the index),

SQL order string as number

China☆狼群 提交于 2019-11-26 03:50:16
问题 I have numbers saved as VARCHAR to a MySQL database. I can not make them INT due to some other depending circumstances. It is taking them as character not as number while sorting. In database I have 1 2 3 4 5 6 7 8 9 10... On my page it shows ordered list like this: 1 10 2 3 4 5 6 7 8 9 How can I make it appear ordered by numbers ascending? 回答1: If possible you should change the data type of the column to a number if you only store numbers anyway. If you can't do that then cast your column