473,387 Members | 1,619 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Tricky SELECT and ORDER BY

Dear Group

I wonder whether you can push me in a direction on how to design the
following statement. I'm looking for a SELECT with some tricky ORDER
BY.

The database table looks like this:

MenuID TabText SubTabID TabOrderID
------- ----------- ----------- -----------
1 Main 0 1
2 Cars 0 2
3 Boats 0 3
4 Planes 0 4
5 Pick-Ups 2 1
6 Campers 2 2

The result should look like this:

Main
Cars
Pick-Ups
Campers
Boats
Planes

Notice that 'Pick-Ups' and 'Campers' are a subcategory of 'Cars' and
must appear in the result directly following 'Cars'.

In more detail:
'Main', 'Cars', 'Boats' and 'Planes' are top-level categories and
'Pick-Ups' and 'Campers' are subcategories of 'Cars'. The SubTabID
value of an item identifies to what top-level category a subcategory
belongs.
The TabOrderID specifies in what order the items should be sorted,
e.g. 'Pick-Ups' comes first and 'Campers' second.

Thanks very much for your help & efforts!

Martin
Jul 20 '05 #1
3 1612
In article <72**************************@posting.google.com >,
Martin <th************@hotmail.com> wrote:
Dear Group

I wonder whether you can push me in a direction on how to design the
following statement. I'm looking for a SELECT with some tricky ORDER
BY [...]


Use the Case keyword in the Order By clause.
--
" We gonna charge, we gonna stomp, we gonna march through the swamp
We gonna mosh through the marsh, take us right through the doors"

Jul 20 '05 #2
On 30 Nov 2004 20:26:25 -0800, Martin wrote:
Dear Group

I wonder whether you can push me in a direction on how to design the
following statement. I'm looking for a SELECT with some tricky ORDER
BY.

The database table looks like this:

MenuID TabText SubTabID TabOrderID
------- ----------- ----------- -----------
1 Main 0 1
2 Cars 0 2
3 Boats 0 3
4 Planes 0 4
5 Pick-Ups 2 1
6 Campers 2 2

The result should look like this:

Main
Cars
Pick-Ups
Campers
Boats
Planes

Notice that 'Pick-Ups' and 'Campers' are a subcategory of 'Cars' and
must appear in the result directly following 'Cars'.

In more detail:
'Main', 'Cars', 'Boats' and 'Planes' are top-level categories and
'Pick-Ups' and 'Campers' are subcategories of 'Cars'. The SubTabID
value of an item identifies to what top-level category a subcategory
belongs.
The TabOrderID specifies in what order the items should be sorted,
e.g. 'Pick-Ups' comes first and 'Campers' second.

Thanks very much for your help & efforts!

Martin


Hi Martin,

The following is untested, as you didn't post CREATE TABLE and INSERT
statements:

SELECT a.TabText
FROM MyTable AS a
LEFT JOIN MyTable AS b
ON b.MenuID = a.SubTabID
ORDER BY b.TabOrderID, a.TabOrderID

Note that this works if you have only one level of subcategories. If the
subcategory of Campers if divided further, my query will fail.

If you need a solution that works with more levels of subcategories, you
should consider using another table design. Your design (the adjacency
list model) is not particularly well suited for relational operations; the
nested sets model is often a better choice for modelling hierarchies.

If you google for "nested sets model Joe Celko", you should find several
explanatory messages on the nested sets model.

Best, Hugo
--

(Remove _NO_ and _SPAM_ to get my e-mail address)
Jul 20 '05 #3
Dear Hugo

Thanks very much for this detailed reply. It even answered a second
question I hadn't asked yet.

Thanks again for your time & efforts!
Have a nice day!

Martin

Hugo Kornelis <hugo@pe_NO_rFact.in_SPAM_fo> wrote in message news:<oc********************************@4ax.com>. ..
On 30 Nov 2004 20:26:25 -0800, Martin wrote:
Dear Group

I wonder whether you can push me in a direction on how to design the
following statement. I'm looking for a SELECT with some tricky ORDER
BY.

The database table looks like this:

MenuID TabText SubTabID TabOrderID
------- ----------- ----------- -----------
1 Main 0 1
2 Cars 0 2
3 Boats 0 3
4 Planes 0 4
5 Pick-Ups 2 1
6 Campers 2 2

The result should look like this:

Main
Cars
Pick-Ups
Campers
Boats
Planes

Notice that 'Pick-Ups' and 'Campers' are a subcategory of 'Cars' and
must appear in the result directly following 'Cars'.

In more detail:
'Main', 'Cars', 'Boats' and 'Planes' are top-level categories and
'Pick-Ups' and 'Campers' are subcategories of 'Cars'. The SubTabID
value of an item identifies to what top-level category a subcategory
belongs.
The TabOrderID specifies in what order the items should be sorted,
e.g. 'Pick-Ups' comes first and 'Campers' second.

Thanks very much for your help & efforts!

Martin


Hi Martin,

The following is untested, as you didn't post CREATE TABLE and INSERT
statements:

SELECT a.TabText
FROM MyTable AS a
LEFT JOIN MyTable AS b
ON b.MenuID = a.SubTabID
ORDER BY b.TabOrderID, a.TabOrderID

Note that this works if you have only one level of subcategories. If the
subcategory of Campers if divided further, my query will fail.

If you need a solution that works with more levels of subcategories, you
should consider using another table design. Your design (the adjacency
list model) is not particularly well suited for relational operations; the
nested sets model is often a better choice for modelling hierarchies.

If you google for "nested sets model Joe Celko", you should find several
explanatory messages on the nested sets model.

Best, Hugo

Jul 20 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Bung | last post by:
Hi, I have a tricky sql statment I have to write (tricky for me) and I am stuck. I'm having trouble with the following problem. Table1 (Column a, Column b, Column c) Table2 (Column a, Column...
3
by: Jeff | last post by:
I have a SQL7 database that was installed as case-insensitive. /* Sort Order = 52, Case-insensitive dictionary sort order. */ This database contains a table that has a varchar column which...
5
by: scott | last post by:
Hello, I'm trying to find the most optimal way to perform a tricky query. I'm hoping this is some sort of standard problem that has been solved before, but I'm not finding anything too useful so...
6
by: pointBoarder | last post by:
Thanks in advance to all who read this. I've got 3 tables which were created from a txt file dumped from some old system. Header ID -- autonumber, primary OrderNum -- field I want Line
8
by: Ken Capriell | last post by:
I know that subject probably did not adequately explain anything so here goes... I have an access file that needs to be transposed from its current format to a new format so that I can then...
3
by: jl | last post by:
Hi everybody! I have an interesting problem - and so far, I haven't been able to solve it. Maybe somebody else has an idea... I have a source xml structure that comes from a legacy system. It...
1
by: MorrganMail | last post by:
Or at least I find it tricky. :-) Assume we have three tables A, B and C. Table A contains a path and the distance for traveling that path: A (PathId, NodeId, Dist (from previous node)) 1, 1,...
15
by: edouard.spooner | last post by:
Hi, I have a tricky SQL query problem that I'm having probs with. I have a table which resembles something like this Date | Price1 | Price2 | Price3 01 Jan 2006 | 100 | 100 | 100 02 Jan...
3
by: snoonan | last post by:
The company in quesiton does construction work. Tables look like this: ***Job Table*** JobNumberID* JobName ***JobNote Table*** JobNoteID* JobNumberID* JobNoteCreateDate
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.