Home > Ask the Oracle Experts > SQL Questions & Answers > Format query output as header and detail lines
Ask The Oracle Expert: Questions & Answers
EMAIL THIS

Format query output as header and detail lines

Rudy Limeback EXPERT RESPONSE FROM: Rudy Limeback

Pose a Question
Other Oracle Categories
Meet all Oracle Experts
Become an Expert for this site


Digg This!    StumbleUpon Toolbar StumbleUpon    Bookmark with Delicious Del.icio.us   


>
QUESTION POSED ON: 14 May 2003

I am using Microsoft Access 2000, and I want to write a query in VB6 using SQL statements to display the department name only once, and then all the employee names working under that department. For example:

department A
    employee A1
    employee A2
      ...
    employee An

department B
    employee B1
    employee B2
      ...
    employee Bm

>
EXPERT RESPONSE

This is one of the more frequent SQL questions. The answer is: don't try to do this with SQL only. Formatting is best done in the application program. I don't know VB6, but I'm sure it's completely adequate for this task.

The query is very simple. Just return both "header" and "detail" columns, and make sure you sort the result set by at least the header column:

select deptname
     , emplname
  from yourtable
order
    by deptname
     , emplname

Your result set will look like this:

department A  employee A1
department A  employee A2
   ...
department A  employee An
department B  employee B1
department B  employee B2
    ...
department B  employee Bm

Now process the result set with application code, using "current/previous" logic to determine when to print a heading (this is pseudocode):

set prevdept = ''
while more rows
  begin 
    if dept > prevdept
      print dept header
      set prevdept = dept
    endif
    print empl detail
    get next resultset row
  end

Reeder feedback

Reader Frank Kulash responds:

"Formatting is best done in the application program". That's true, but it's not always possible. Here's one solution for those rare occasions when you have to do the job in SQL.

It's easy to produce all the header lines in one query, and all the detail lines in another query. But how do you combine them in one result set? That's exactly what UNION does.

  SELECT DISTINCT deptname
  FROM    yourtable
UNION
  SELECT  emplname
  FROM    yourtable;
(By the way, this works equally well if the header and detail lines come from different tables.) Okay, now we've got all the right lines, but how do we get them into the right order? Say we want departments sorted alphabetically, then, within each department, the header followed by an alphabetical list of employees. Add sorting columns to the query above.
  SELECT DISTINCT
          deptname  AS sort1
        , 1         AS sort2
        , deptname   AS display_text
  FROM    yourtable
UNION
  SELECT  deptname  AS sort1
        , 2         AS sort2
        , emplname  AS display_text
  FROM    yourtable
ORDER BY  1, 2, 3;
(In this case, there's no need for a sort3 dummy column, since display_text works as well. Department names and employees names won't get mixed together becuase the 1 in sort2 guarantees that the department name appears before any of the employee names.)

Now the only problem is that we've got these ugly sorting columns in the output. We need them for sorting, but we don't want to look at them. Here's one way (at least in Oracle, I don't know about MSAccess) to hide the sort columns.

SELECT  display_text
FROM
(
  SELECT DISTINCT
          deptname  AS sort1
        , 1         AS sort2
        , deptname   AS display_text
  FROM    yourtable
UNION
  SELECT  deptname  AS sort1
        , 2         AS sort2
        , emplname  AS display_text
  FROM    yourtable
)
ORDER BY  sort1
        , sort2
        , display_text;


Digg This!    StumbleUpon Toolbar StumbleUpon    Bookmark with Delicious Del.icio.us   


RELATED CONTENT
SQL
Finding a column value inside a user-supplied string
Update a specific column in a field or row?
Using BETWEEN with DATETIMEs in SQL
Which normal form is used most?
IN list or series of OR conditions?
Connecting tables in a database
SQL query for co-authored books
Querying complex derived tables
SQL string functions
Changing a NULL column to NOT NULL

RELATED RESOURCES
2020software.com, trial software downloads for accounting software, ERP software, CRM software and business software systems
Search Bitpipe.com for the latest white papers and business webcasts
Whatis.com, the online computer dictionary



Search and Browse the Expert Answer Center
Search and browse more than 25,000 question and answer pairs from more than 250 TechTarget industry experts.
Browse our Expert Advice

HomeNewsTopicsTipsAsk the ExpertsMultimediaWhite PapersProductsBlogs
About Us  |  Contact Us  |  For Advertisers  |  For Business Partners  |  Site Index  |  RSS
SEARCH 
TechTarget provides enterprise IT professionals with the information they need to perform their jobs - from developing strategy, to making cost-effective IT purchase decisions and managing their organizations' IT projects - with its network of technology-specific Web sites, events and magazines.

TechTarget Corporate Web Site  |  Media Kits  |  Reprints  |  Site Map




All Rights Reserved, Copyright 2003 - 2008, TechTarget | Read our Privacy Policy
  TechTarget - The IT Media ROI Experts