Reputation: 2227
Is it possible to use the names of the actual columns for the order by clause?
I am using a view to let a client use a reporter writer (Pentaho) and this would make things easier on them.
To clarify, I want to put the results in alphabetical order of the column names themselves. I want to sort the data using the columns, not the data IN the columns.
Upvotes: 1
Views: 4726
Reputation: 2354
In PostgreSQL you can try:
SELECT column_name, data_type FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME =
'my_table' ORDER BY column_name;
Upvotes: 2
Reputation: 1904
You can create the view with the columns in any order you like. Then SELECT * FROM your_view
queries will return the columns in the order specified by the view.
Upvotes: 0
Reputation: 75916
If what you mean is to change the order of the columns themselves according to their names (that would make sense only if you are using SELECT *
, I guess), I'm afraid that's not possible, at least not straightforwardly. And that sounds very unSQL, I'd say...
Upvotes: 2
Reputation: 8870
Sure, you can order by column name, column alias, or column position:
select a, b from table order by b;
select a as x, b as y from table order by x, y;
select a, b from table order by 1;
Upvotes: 1