1
0
mirror of https://github.com/jbranchaud/til synced 2026-01-03 07:08:01 +00:00

Add Use Argument Indexes as a postgres til.

This commit is contained in:
jbranchaud
2015-07-01 20:06:55 -05:00
parent df4be8207c
commit b54a6bd807
2 changed files with 25 additions and 0 deletions

View File

@@ -0,0 +1,24 @@
# Use Argument Indexes
In Postgres, each of the arguments you specify in a `select` statement has a
1-based index tied to it. You can use these indexes in the `order by` and
`group by` parts of the statement.
Instead of writing
```sql
select id, updated_at from posts order by updated_at;
```
you can write
```sql
select id, updated_at from posts order by 2;
```
If you want to group by a table's `type` and then order by the counts from
highest to lowest, you can do the following
```sql
select type, count(*) from transaction group by 1 order by 2 desc;
```