Suppose that I have a database table that contains information on cities across the United States. My display shows information first grouped by State, then County, and finally by City something like below:
- Alabama
- Autauga County
- city 1
- city 2
- etc
- Baldwin County
- city 1
- city 2
- etc
- County etc
- Alaska
- Alaska county 1
- city 1
- alaska county 2
The query would look something like:
select * from myInfo order by state, county, city
A new wrinkle is that all states that contain cities called 'Lincoln' should have their states and counties ordered first. States without cities named Lincoln are to be ordered after. The desired result is something like:
- Nebraska
- Lancaster County
- Lincoln
- some other Lancaster City
- Some other Nebraska County
- New Mexico
- Lincoln County
- Lincoln
- some other city
- some other county
- A Non-Lincoln State
I could do this complex ordering in code but I'm wondering how difficult it is to implement in pure sql. How difficult is the resulting query?