sql-order-by

How to efficiently sort by the results of a subquery?

北城余情 提交于 2020-04-17 22:10:25
问题 Let's say I have a site like Stackoverflow with posts that people can reply to, and I would like to have an interface for sorting the posts by reply count This will eventually have infinite scroll pagination, so showing 10 results at a time. Here's an example query for that: SELECT *, (SELECT COUNT(*) FROM post_reply pr WHERE pr.convo_id = post.convo_id) as replies FROM post ORDER BY replies LIMIT 10; This works, but it is prohibitively slow. I have hundreds of thousands of posts and this

Ordering by a field not in the select statement in SQL

时光怂恿深爱的人放手 提交于 2020-03-16 05:49:41
问题 I need to create a query that pulls only the customer_no column (because the software restrictions are as such, and I can't code it externally). But I need to be able to sort the data by create_dt (in reverse) column. The code/SQL is restricting me in using the following because in order to sort by something that data has to appear int the select statement. I can't have it appear there – is there any way around this? Select Distinct top 3500 a.customer_no From T_CUSTOMER a WITH (NOLOCK) JOIN

Performing an operation on the result of a query, without saving a table

江枫思渺然 提交于 2020-03-04 18:35:30
问题 I'm very new to SQL and T-SQL, so please forgive any mistakes in terminology or if the answer is obvious - I don't even know where to start googling for the solution to this problem. I only have SELECT permission on a T-SQL server. I have a query that pulls a bunch of information based on an event with a Start Date (DATETIME) and End Date (DATETIME). I want to have a calculated column in my query result that indicates whether the person was just involved in back-to-back events. So, something

Partition By with Order By Clause in PostgreSQL

大兔子大兔子 提交于 2020-02-28 15:44:06
问题 I have a table with these values; user_id ts val uid1 19.05.2019 01:49:50 0 uid1 19.05.2019 01:50:15 0 uid1 19.05.2019 01:50:20 0 uid1 19.05.2019 01:59:50 1 uid1 19.05.2019 02:20:10 1 uid1 19.05.2019 02:20:15 0 uid1 19.05.2019 02:20:19 0 uid1 19.05.2019 02:30:53 1 uid1 19.05.2019 11:10:25 1 uid1 19.05.2019 11:13:40 0 uid1 19.05.2019 11:13:50 0 uid1 19.05.2019 11:20:19 1 uid2 19.05.2019 15:01:44 0 uid2 19.05.2019 15:05:55 0 uid2 19.05.2019 17:19:35 1 uid2 19.05.2019 17:20:01 0 uid2 19.05.2019

FETCH/ROWNUM() first n percent from each BRANCH (BRANCH_NO). I am trying to retrieve top 10 percent of each branch

被刻印的时光 ゝ 提交于 2020-02-25 06:03:39
问题 SELECT e.EMPLOYEE_NO, e.FNAME, e.LNAME, b.BRANCH_NO, o.SUBTOTAL, PERCENT_RANK() OVER ( partition by e.EMPLOYEE_NO ORDER BY e.EMPLOYEE_NO ASC) AS percent FROM EMPLOYEE e INNER JOIN BRANCH b ON e.BRANCH_NO = b.BRANCH_NO INNER JOIN ORDERS o ON o.BRANCH_NO = b.BRANCH_NO ORDER BY b.BRANCH_NO FETCH FIRST 10 PERCENT ROWS ONLY; I am trying to retrieve top 10 percent of each branch. PL SQL 回答1: You can use analytical functions as following: Select employee_no, Fname, Lname, employee_total_order,

Strange TSQL behavior with COALESCE when using Order By [duplicate]

怎甘沉沦 提交于 2020-02-16 05:21:06
问题 This question already has answers here : nvarchar concatenation / index / nvarchar(max) inexplicable behavior (2 answers) Closed 4 years ago . I'm having some very strange behavior with coalesce. When I don't specify a return amount (TOP (50)) I'm only getting a single last result, but if I remove the "Order By" it works... Examples below DECLARE @result varchar(MAX) SELECT @result = COALESCE(@result + ',', '') + [Title] FROM Episodes WHERE [SeriesID] = '1480684' AND [Season] = '1' Order by

Strange TSQL behavior with COALESCE when using Order By [duplicate]

[亡魂溺海] 提交于 2020-02-16 05:15:43
问题 This question already has answers here : nvarchar concatenation / index / nvarchar(max) inexplicable behavior (2 answers) Closed 4 years ago . I'm having some very strange behavior with coalesce. When I don't specify a return amount (TOP (50)) I'm only getting a single last result, but if I remove the "Order By" it works... Examples below DECLARE @result varchar(MAX) SELECT @result = COALESCE(@result + ',', '') + [Title] FROM Episodes WHERE [SeriesID] = '1480684' AND [Season] = '1' Order by

Laravel - randomly select n number of rows containing same value in certain column after applying 'order by'

▼魔方 西西 提交于 2020-02-06 09:27:13
问题 In my Laravel project, in the database table ads , I have the following structure : id | col1 | col2 col2 has values like topad , bump , urgent along with empty value. I want to take all the rows from the ads table and sort them alphabetically based on col2 in descending order. So I used: Ads::orderBy('col2','DESC')->get() Now I have 2 conditions to be applied on the query. 1st condition : Suppose there are 4 rows with topad in col2 , 5 rows with urgent in col2 , 6 rows with bump in col2 and

Laravel - randomly select n number of rows containing same value in certain column after applying 'order by'

烂漫一生 提交于 2020-02-06 09:23:30
问题 In my Laravel project, in the database table ads , I have the following structure : id | col1 | col2 col2 has values like topad , bump , urgent along with empty value. I want to take all the rows from the ads table and sort them alphabetically based on col2 in descending order. So I used: Ads::orderBy('col2','DESC')->get() Now I have 2 conditions to be applied on the query. 1st condition : Suppose there are 4 rows with topad in col2 , 5 rows with urgent in col2 , 6 rows with bump in col2 and

how to get value from mysql table ordered by another table?

╄→尐↘猪︶ㄣ 提交于 2020-02-05 08:56:11
问题 i have some reference to record for a given article. a reference might be of type an article, book, thesis etc.. each might have different attributes, e.g. //article_refs table ID Article_ID Article_Title Author_Name ... 1 1 title1 author1 2 1 title2 author2 //thesis_refs table ID Article_ID Thesis_Title Thesis_Publisher ... 1 1 thesis1 publisher1 2 1 thesis2 publisher2 //ref_types table ID Article_ID ReferenceType 1 1 book 2 1 article 3 1 article 4 1 book when i insert into one of the tables