Suppose I have a table NAME with the columns PART_TYPE and VALUE. Each type can have multiple types like FAMILY, GIVEN, PREFIX, etc.
What I would like to do is write SQL to get all the names but have them first ordered by a specific type then have them ordered alphabetically within that type.
I have something like this:
SELECT *
FROM name
ORDER BY (
CASE
WHEN PART_TYPE = 'GIV'
THEN VALUE
END)
But I’m not sure how to include the other types as a primary order while keeping the VALUE as the secondary order. Adding another case statement will then just make VALUE the primary order.
I would like to keep this database independent but if need be, I would be willing to make it Oracle specific.
How can I do this? Also, a way of doing this with Hibernate Criteria would also be greatly appreciated.
Edit: I need the PART_TYPE to be ordered by a specific order as defined by me. In particular, it needs to be GIVEN then FAMILY.
So it should look like this:
GIVEN A
GIVEN B
GIVEN C
FAMILY A
FAMILY B
The natural order by would have FAMILY first, not GIVEN.
The most manageable way of resolving this is probably to have a table containing all your Types, and then a sort column within that. So you might have a table like:
Then you Can join this on to your query and use:
You could just resolve this with a
CASEexpressionBut the more types you have the less manageable the case expression becomes.