I want to ORDER BY the case statement, is it possible? How can I do it?
SELECT new com.systemname.to.UserDataHolder(u.userId,
CASE WHEN (u.place.cityId = :cityId) THEN 1 WHEN (u.place.stateId = :stateId) THEN 2 ELSE 3 END)
FROM User u
ORDER BY u.userId DESC
What JPA provider are you using?
Try,
SELECT u.userId,
(CASE
WHEN (u.place.cityId = :cityId) THEN 1
WHEN (u.place.stateId = :stateId) THEN 2
ELSE 3 END) as myNum
FROM User u
ORDER BY u.userId, myNum DESC
or,
SELECT new com.systemname.to.UserDataHolder(u.userId,
CASE
WHEN (u.place.cityId = :cityId) THEN 1
WHEN (u.place.stateId = :stateId) THEN 2
ELSE 3 END)
FROM User u
ORDER BY u.userId, CASE
WHEN (u.place.cityId = :cityId) THEN 1
WHEN (u.place.stateId = :stateId) THEN 2
ELSE 3 END DESC
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With