Ordering by a field not in the select statement in SQL

后端 未结 3 1946
独厮守ぢ
独厮守ぢ 2021-01-19 18:49

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 so

3条回答
  •  长情又很酷
    2021-01-19 19:08

    Use a subquery that includes the column you're ordering by. Then the main query can just return the column you care about:

    SELECT customer_no
    FROM (
        Select top 3500 a.customer_no, a.create_dt
          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
          order by a.create_dt desc
    )
    

提交回复
热议问题