# Using a CASE expression to order results

**URL:** https://meta.discourse.org/t/using-a-case-expression-to-order-results/275148
**Category:** Data & reporting
**Tags:** sql-query
**Created:** [March 1, 2019, 8:39pm UTC](https://meta.discourse.org/t/using-a-case-expression-to-order-results/275148 "2019-03-01T20:39:18Z")
**Posts on this page:** 1
**Page:** 1

<div class="post-metadata">

### Author: ![simon](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/simon/32/339122_2.png) [@simon](https://meta.discourse.org/u/simon)
#### Post date: [March 1, 2019, 8:39pm UTC](https://meta.discourse.org/t/using-a-case-expression-to-order-results/275148/1 "2019-03-01T20:39:18Z")

</div>

### Using a CASE expression to order results

I don’t think it’s possible to pass a keyword as a parameter, but a boolean `:desc` parameter could be used in a `CASE` expression.

```sql
--[params]
-- boolean :desc = false

SELECT
*
FROM generate_series(1, 10) AS num
ORDER BY
  CASE WHEN :desc THEN num END DESC,
  CASE WHEN NOT :desc THEN num END ASC

```
