I want to use an alias for the order by clause in ...

# exposedj

Jeff

06/10/2021, 9:17 AMI want to use an alias for the order by clause in Exposed Kotlin like so:

Copy code

`6371 * ACOS(COS(RADIANS(:latitude)) * COS(RADIANS(`lat`)) * COS(RADIANS(:longitude) - RADIANS(`lng`)) + SIN(RADIANS(:latitude)) * SIN(RADIANS(`lat`))) AS `distance``

Anyone with an idea on how to implement this?s

spand

06/10/2021, 10:38 AMUse the alias function

e

Endre Deak

06/10/2021, 10:42 AM`alias.aliasOnlyExpression`

could be usefulj

Jeff

06/10/2021, 2:56 PMHow exactly **@spand** **@Endre Deak**

Jeff

06/14/2021, 10:00 AMAnyone? Not sure how to use the alias function in this specific usecase.

e

Endre Deak

06/14/2021, 10:27 AMwhatâ€™s your full query

Endre Deak

06/14/2021, 10:27 AM?

j

Jeff

06/14/2021, 10:32 AMTo simplify, I'm getting a list of offices. I want to order them by location

Jeff

06/14/2021, 10:34 AMSomething like:

Copy code

```
Offices
.leftJoin(Doctors, { Offices.doctor }, { Doctors.id })
.slice(
...,
...,
).selectAll().limit(20).orderBy(...)
```

Jeff

06/14/2021, 10:51 AMI know I can have the calculations in Kotlin like so but I still don't understand how to use the alias function to order the query.

Copy code

```
val distance = 6371 *
Math.acos(
Math.cos(Math.toRadians(myLatitude)) *
Math.cos(Math.toRadians(officeLatitude)) *
Math.cos(
Math.toRadians(officeLongitude) -
Math.toRadians(myLongitude)
) +
Math.sin(Math.toRadians(myLatitude)) *
Math.sin(Math.toRadians(officeLatitude))
)
```

Jeff

06/14/2021, 11:09 AMHow do you think i can use alias.aliasOnlyExpression **@Endre Deak**?

s

spand

06/14/2021, 12:39 PMIf you really want the database to do that then you need to convert the math expression to an exposed expression like you would in the

`.select { }`

blockj

Jeff

06/14/2021, 1:09 PMI would really need an example

t

tapac

06/14/2021, 5:56 PMAs there is no such functions like acos/cos/etc in Exposed (and it would be great if you'll contribute them:) first of all you need to declare such functions:

Copy code

```
fun CustomDoubleFunction(
functionName: String,
vararg params: Expression<*>
): CustomFunction<BigDecimal> = CustomFunction(functionName, DoubleColumnType(), *params)
fun Radiants(exp: Expression<BigDecimal>) = CustomDoubleFunction("RADIANTS", exp)
```

after that when you'll have all needed functions you can write:
Copy code

`val distance = Acos(Cos(Radiants(MyTable.lattitue))) * ...`

e

Endre Deak

06/14/2021, 6:23 PMðŸ‘†
A bunch of those functions would do that.

14 Views