Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ordering by a field not in the select statement in SQL

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 (Select a1.customer_no From VXS_CUST_TKW a1 WITH (NOLOCK) Where a1.tkw in (141)) as e ON      e.customer_no = a.customer_no
  Where 1 = 1
 order by a.create_dt desc
like image 518
YelizavetaYR Avatar asked Sep 11 '14 14:09

YelizavetaYR


People also ask

Can you order by a field that is not in select statement?

Yes, you can order by a field (s)even if it is not your in your select statement but exists in your table. For a group by clause though you'd need it to be in your select statement. is it possible to give an SELECT statement ...ORDER BY but the field in ORDER BY clause would not be in SELECT list.

How to order by a column in a select list?

In other words, if we want to order by a column, that column must be specified in the SELECT list. The rule checks for ORDER BY clauses that reference select list columns using the column number instead of the column name. The column numbers in the ORDER BY clause impairs the readability of the SQL statement.

Where does the ORDER BY clause Go in SQL?

In this syntax, the ORDER BY clause appears after the FROM clause. In case the SELECT statement contains a WHERE clause, the ORDER BY clause must appear after the WHERE clause. To sort the result set, you specify the column in which you want to sort and the kind of the sort order: Ascending ( ASC)

What happens if you don’t specify the sort order in SQL?

If you don’t specify the sort order, the database system typically sorts the result set in ascending order ( ASC) by default. When you include more than one column in the ORDER BY clause, the database system first sorts the result set based on the first column and then sort the sorted result set based on the second column, and so on.


1 Answers

Of course you can. Your query looks like SQL Server, where this will likely do what you want:

  Select top 3500 a.customer_no 
  From T_CUSTOMER a  WITH (NOLOCK) JOIN
       (Select a1.customer_no
        From VXS_CUST_TKW a1 WITH (NOLOCK)
        Where a1.tkw in (141)
       ) e
       ON e.customer_no = a.customer_no
  Where 1 = 1
  group by a.customer_no
  order by max(a.create_dt) desc;

The equivalent query in MySQL would look like:

  Select a.customer_no 
  From T_CUSTOMER a JOIN
       (Select a1.customer_no
        From VXS_CUST_TKW a1 
        Where a1.tkw in (141)
       ) e
       ON e.customer_no = a.customer_no
  Where 1 = 1
  order by a.create_dt desc
  limit 3500;

I removed the distinct because it may not be necessary. If it is, add it back in.

like image 131
Gordon Linoff Avatar answered Sep 20 '22 06:09

Gordon Linoff