Convert INT to BOOLEAN in a SQL SELECT for Multi-Tiered Sorting

By M&S Consulting (John) on Jun 7th, 2009
Filed Under Technical Tips // Tags: , , ,

The Challenge

We have a family tree. Some people have children, while others do not. We want to sort results by alphabetically listing individuals with children in an initial group, and then those without children in a second group — also alphabetically. Let’s assume there is not an isParent column (nor the equivalent) on the table, but there is a way to determine how many children each individual has.

The problem is that if we simply sort by the children count of each individual, then their name, it will provide incorrect, undesirable results. It will first show alphabetically those with the most children, then those with the second most, . . ., and finally those individuals with no children at all.

The Solution

This could be accomplished with a SQL Statement like so:

1
2
3
4
5
SELECT thisPerson.lastName, thisPerson.firstName, COUNT(child.id) AS children
FROM person AS thisPerson, person AS child
WHERE thisPerson.id = child.parentId
GROUP BY person.id
ORDER BY children, thisPerson.lastName, thisPerson.firstName

The INT given by the COUNT(child.id) as children is going to mess up our sorting. Instead, we’d like it to be either a 0 or a 1 (i.e. dynamically calculate isParent).

If we think about it, the children count will always be an INT >= 0. Knowing that, we can apply this aggregate formula to convert the count into our desired results in pseudocode:

isParent: Ceiling( children / (children + 1) )
In the case where children is 0, we’d have: Ceiling(0/(0+1)) = Ceiling(0) = 0
In the case where children is 1, we’d have: Ceiling(1/(1+1)) = Ceiling(.5) = 1
In the case where children is 2, we’d have: Ceiling(2/(2+1)) = Ceiling(.667) = 1
In the case where children is 3, we’d have: Ceiling(3/(3+1)) = Ceiling(.75) = 1

We’ll leave it as an exercise for the reader to convert the SQL statement using the above formula. Of course, ask away if you have questions.

Bookmark and Share

Related Information:

  1. Semantics, Reality, Marketing: I’m “Cloud Computing” Too
    If there were a buzz word that could compete with the speed to fame, breadth of use (and misuse), and...
  2. FreeQL - Free Oracle SQL Select Statements
    M&S Consulting has begun a revolutionary service where we will write Oracle SQL statements for FREE. We have named this...

Leave a Reply


Archives

Recent Comments

  • Kavitha Muniraj said: Hi, Could you please send a script which checks the status of forms and reports of oracle...
  • Siddharth said: Hey thanks a lot !! Have been lookin all over for this :) My PC may not be able t handle FlashBuilder...
  • Cyrex said: Hey man why I cant install Apex 4? is there anyone can help me.,?
  • sharanabasava said: ALTER TABLE [my_table_name] RENAME COLUMN [current_column_name] TO [new_column_name]; Sir,I...
  • Huub Vankan said: Thanks for this easy solution. Indeed strange that is does not work in the (x86) folder….

Calendar

July 2010
M T W T F S S
« Jun    
 1234
567891011
12131415161718
19202122232425
262728293031  

Featured Testimonial

"M&S has taken 'partnership' to a new level. They are not only a deeply skilled service provider, the M&S team is actually a mission critical component to our operations, taking on new initiatives as well as maintaining custom, open source, and COTS solutions."

CEO, MedPivot

Free SQL Service