Questions tagged [sql-order-by]
An ORDER BY clause in SQL specifies that a SQL SELECT statement returns a result set with the rows being sorted by the values of one or more columns.
sql-order-by
7,736 questions
0
votes
3
answers
179
views
Trouble with SQL Server query containing DISTINCT, ORDER BY and FOR XML PATH commands [closed]
Here is an extract of table B_TimeEntryMatter.
ProformaNr
ActivDate
Description
451894
17/12/2025
To the provision of a Certificate of Incumbency by the Secretary;
451894
18/12/2025
To time spent in ...
0
votes
0
answers
78
views
Scroll fails while using order by string column in manticore
I’m using Manticore Search 14.1.0 (Docker image manticoresearch/manticore:latest) on Ubuntu 20.04.6 LTS. I rely on cursor-style pagination with OPTION scroll, but when the sort clause includes a ...
5
votes
4
answers
250
views
Sort aggregated query results by two methods simultaneously
I need to sort a query's results by two methods at the same time.
I want the first 3 records (returned) to be based on their prevalence in another table
And then I want the rest of the results sorted ...
-2
votes
1
answer
75
views
MySQL order by id but prioritize an empty value of a certain column
Mainly, I want to order the ids in descending direction but also, how can I sort parent product/items (the ones with empty ean) first and their child rows below them?
Here's an example data from the ...
4
votes
2
answers
179
views
Why does the sort method used by Postgres change from "top-N" to "external merge" when FOR UPDATE is added?
I have a table in Postgres (14) that is used like a queue with FOR UPDATE SKIP LOCKED queries. It was missing an index, but since any rows are usually processed ...
4
votes
1
answer
205
views
Cursor pagination with conditional sorting
I need to implement keyset pagination (sometimes referenced as cursor-based or seek method) in Postgres with the following ordering rules:
Rows where ...
0
votes
2
answers
113
views
How to pick the latest record with GROUP BY userId
In my application I want to find the latest duty of each user from 'StaffDuty' table using hibernate query (i.e. HQL). Below is my query.
...
1
vote
2
answers
72
views
Why aren't my Django Postgres `ArrayAgg` members sorting?
I'm exploring the use of ArrayAgg and I don't understand why 'histidine-[13C6,15N3]' doesn't occur before ...
1
vote
2
answers
93
views
SQL join 2 table with date, filtered and grouped by a week number
I have 3 tables in SQL:
Agency, booking and checkin (and rooms)
I need to compute a % of checkin / booking, by week number (of begin), by agency
Here somes tables for example
Agency :
id
name
1
first
...
0
votes
2
answers
141
views
SQL error: operand type clash: decimal is incompatible with date
I have the following stored procedure:
...
0
votes
2
answers
127
views
Sort rows by latest group
I have the below dataset which includes 3 columns:
Inspection Id
Inspection Date
Fault Number
Note: I have added in the 2 additional columns 'Comment' and 'Required Sort Order' to help explain what ...
0
votes
0
answers
57
views
Wordpress admin custom post list page orderby meta_value_num doesn't work
Been trying to order the custom post type listing page based on a custom meta field.
the meta field value would be in the form of string and number (pattern: '[string][unsigned]')(examples: 'A1','A2',....
2
votes
2
answers
141
views
SELECT ... ORDER BY <boolean condition>
Having this situation:
SELECT DISTINCT "FieldName"
FROM "TableName"
ORDER BY "FieldName" ASC;
I'd like to have the lines containing ...
1
vote
2
answers
129
views
Postgres inner join where and order by
Is there a way to efficiently order by a text field in a join query (i.e., fast on large datasets)? I understand that I probably need to filter the dataset somehow, to reduce the size of the dataset ...
1
vote
2
answers
97
views
How to correct the syntax for ORDER BY that works but the CASE logic is backwards?
I have the following folders table:
...
0
votes
1
answer
94
views
Does MySQL return results ordered by id when using LIMIT with an increasing id condition? [duplicate]
I’m optimizing a query for better performance, and I use the following SQL statement:
...
0
votes
1
answer
62
views
Oracle: Ensuring SELECT FOR UPDATE Locks Rows in a Specific Order Before Selection
I have a query in Oracle where I need to:
Order the entire table by a specific column.
Select and lock a certain number of rows using SELECT FOR UPDATE.
However, the issue is that Oracle first selects ...
1
vote
0
answers
69
views
AWS Redshift unload not respecting order by
I defined a redshift table (some fields omitted from this sample) with this DDL:
...
1
vote
1
answer
151
views
More than one OrderBy clause in EF Core per Paramter Func<T,obj> and how to build the query more generic
I have a base razor component, that loads data from a SQL database. To pass a sort order to this component, there are two "sort" Func<T,object> parameters and two bool to set asc/desc.
...
0
votes
2
answers
121
views
Order by subset of related field?
I have a model Release. Each release has a type, and depending on that type different types of Credit are considered primary ...
2
votes
6
answers
121
views
SQL ASC and DESC ordering on same column based on another column
I want to arrange pending orders by eta in asc order and completed orders in desc order, with all completed orders appearing at the bottom of the list. Not sure if this is possible in a single query.
...
-2
votes
1
answer
106
views
Order by ASC doesn't return any item but order by DESC does
Any known explanation of this? Why Order By ASC doesn't return any item but order by DESC doesn't?
I am using postgres with pg_vector extension
...
-2
votes
3
answers
85
views
<sqlite> - Order by a specific pattern?
I would like to get my entries ordered by a specific pattern.
For example I have an array like this
arr = [2,5,3,9,4]
Now I want to get the entries from my db by ...
0
votes
0
answers
66
views
HQL ORDER BY query with adding days to date column
I use spring boot, and I want to add specific days to date column in mysql database on hql query order by clause. But which results error when I tried. I need to get results in the order of adding ...
-2
votes
1
answer
54
views
Yii2 Active Record Order By Primary Key ID not working [closed]
I have a pretty basic Yii2 ActiveRecord query.
$paperList = Paper::find()->orderBy(['id' => SORT_DESC])->all();
id is my primary key column which is of ...
-1
votes
2
answers
57
views
Does order by in a window function add more frames?
Using :
sum(Age) over(partition by Country) as Total
...
0
votes
1
answer
121
views
SQL Group By and Order by with sum
I have this query:
...
1
vote
0
answers
57
views
How do I group results of a query in wordpress
I've got a custom post type for staff. The fields are first_name, last_name, job_title, telephone, email, and notes. All staff are categorized by location using WP core Category. I need to output the ...
0
votes
2
answers
69
views
Does using ORDER BY on an indexed column in a query cause issues if new rows are inserted during query execution
I have a table Users with id, this table has a million records or more. So when fetching data from the table I am using batching with offset & limit to reduce ...
0
votes
1
answer
229
views
Having difficulty sorting Snowflake data that contains numeric and non-numeric data
I have a query in Snowflake where I am returning both numeric and varchar values and am struggling with getting the sort to work correctly.
For context, this column is calculated by dividing two ...
1
vote
3
answers
138
views
SQL query left join with sum,group by and order by in MS Access
I'm trying to execute a SQL query with a left join with sum, group by and ...
0
votes
3
answers
147
views
How to sort all numbers before letters with linguistic sorting?
In an Oracle SQL query which uses binary sorting rules, numbers are sorted before letters. But if the query uses a language specific collation to enable linguistic sorting, then numbers are sorted ...
1
vote
3
answers
124
views
How to sort numbers before letters with linguistic sorting?
In an Oracle SQL query which uses binary sorting rules, numbers are sorted before letters. But if the query uses a language specific collation to enable linguistic sorting, then numbers are sorted ...
0
votes
1
answer
70
views
Can I order by a conditional combination of fields in MySQL?
Example Fiddle
...
0
votes
1
answer
80
views
How select query from record count more than 1 and count more than 2 with custom column (Option) in MS Access
I'm trying to select query from record count more than 1 (ID&DATE&INOUT) and count more than 2 (ID&DATE) with custom column (Option) in MS Access
so with "NO" in the option ...
1
vote
1
answer
105
views
Why are Oracle and SQL Server producing different sort orders?
I've got a table with a column of BANK_ID alphanumeric (VARCHAR2 in Oracle, VARCHAR in SQL ...
1
vote
2
answers
173
views
How select query from record count more than 2 with custom column (Option) in MS Access
I'm trying to select query from row count more than 2 based on ID and DATE and OPTION in MS Access.
so with "NO" in the option column then the record more than 2 is not included in my sql ...
1
vote
1
answer
87
views
How select query from record count more than 2 in MS Access
I'm trying to select query from row count more than 2 based on ID and DATE in MS Access.
Please Guide me
I want to keep it as a single query.
Thanks
Table Absen
ID
...
0
votes
2
answers
78
views
SAS proc sql: select for each value the right interval
Suppose there is a list of clients and each client has different contracts that have a due date. For each client, I want to select the contract with the first following duedate after a given reference ...
0
votes
1
answer
93
views
Why Isn't SQLAlchemy's order_by Sorting URLs?
I'm working on a Flask application using SQLAlchemy where I need to consistently sort URLs stored in a database. The goal is to have a consistent ordering of competitor URLs so that when comparing ...
0
votes
4
answers
77
views
-1
votes
1
answer
55
views
PostgreSQL Custom Order By in Java CriteriaBuilder
Id
Child_one_id
Child_two_id
Parent_id
1
2
3
null
2
null
null
1
3
2
null
1
4
null
null
null
5
6
7
null
6
null
null
5
7
6
null
5
8
null
null
null
9
null
null
null
10
11
12
null
11
null
null
10
12
null
...
0
votes
3
answers
61
views
Different sort order result on two different clients
I get different sorting results when trying the same select on two different clients.
This is a simple example:
...
1
vote
2
answers
41
views
Oracle SQL Grouping same value records in particular order
I have simple table of Time and Task columns where it is ordered by Time and has associated task to it as per below example.
Question: How to group the tasks and get the earliest time of every time ...
0
votes
1
answer
162
views
C# sorted get different result from ORDER BY in SQL Server
I need to replicate the exact order produced by another program that used the ORDER BY clause in its SQL query:
...
1
vote
1
answer
283
views
How to order by SUM() DESC when using WITH ROLLUP?
I want to use SELECT WITH ROLLUP in MariaDB to get a summary row in the result set, but also order the remaining rows by ...
1
vote
0
answers
57
views
JPA CriteriaQuery: using same expression in select and orderBy
I tried to write belowed SQL with CriteriaAPI in Hibernate 6.2.6:
...
1
vote
0
answers
67
views
How to OrderBy entity results using Map value included in the @Entity as @CollectionTable
I am trying to orderby value of a Map that is included in my @Entity as @CollectionTable @ElementCollection. I try to do this using criteriabuilder and eclipselink.
I provide below the entity example
<...
0
votes
1
answer
47
views
Use of GROUP BY with ORDER BY in MYSQL
My written query is:-
Query 1:
...
0
votes
0
answers
55
views
Why the ORDER BY clause works like this? [duplicate]
I need help to find out why this behaviour (not a solution). It's about the ORDER BY clause in SQL Server. It has nothing to do with the related post, because my case it's not about case sensitive/...