Benefits Of Using SQL Ordinal Position Notation?

前端 未结 6 2101
夕颜
夕颜 2020-11-30 05:46

Background Information

Ordinal position notation, AKA ordinals, is column shorthand based on the column order in the list of columns in the SELECT cla

6条回答
  •  难免孤独
    2020-11-30 06:17

    Often times when I'm querying a table with a lot of columns (in ad-hoc-land just for data exploration... I would never code like this for a PROD environment) I do something like this to get fields I care about close together:

    select top 1000
      Col_1, Col_18, Col_50, Col_117, *
    from
      TableWithTonsOfCols
    order by
      1, 4 desc, 3
    

    If I said order by Col_1, Col_117 desc, Col_50 my query would barf because the statement wouldn't know which columns I meant to order by due to the " * " doubling up. Not very common, but still a useful feature.

提交回复
热议问题