""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
SQL TIP: LIST IN TWO-COLUMN FORMAT
""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
PRODUCT : R:BASE VERSION : 3.1
CATEGORY : SQL SUBCATEGORY : TWO-COLUMN FORMAT
""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
Replace programs, complex reports, and relational commands with SQL to
achieve maximum performance.
It's often convenient to use SQL to print off a quick list--like a
list of phone numbers--in telephone book style (sorted down one column
and then the other). In the past, we've shown you how to do this using
a program or a complex report. Now, it's easy and fast to do in SQL.
Modify the example shown here to work with your table and column
names. This example uses a table named SALES. The columns FNAME,
LNAME, and PHONE hold the first name, the last name, and the telephone
number without the area code. An INTEGER column (ROWNUM) is numbered
by the AUTONUM command to provide the basis for determining which
column gets which row. You may need to add ROWNUM to your table before
using this little SQL program.
Here's the program (SQL2COL.CMD):
*( SQL2COL.CMD--list names and phone)
*( numbers in two columns sorted down one)
*( list and then the other.)
AUTONUM rownum IN sales USING 1 +
ORDER BY lname NUM
SELECT COUNT(*) INTO vcnt FROM sales
IF (MOD(.vcnt,2))=0 THEN
SELECT (t1.lname + ',' & t1.fname)=16, +
'.....'=5, t1.phone=8, ' '=10, +
(t2.lname + ',' & t2.fname)=16, +
'.....'=5, t2.phone=8 +
FROM sales t1, sales t2 WHERE +
t2.rownum = (t1.rownum + .vcnt / 2) +
ORDER BY t1.rownum
ELSE
SELECT (t1.lname + ',' & t1.fname)=16, +
'.....'=5, t1.phone=8, ' '=10, +
(t2.lname + ',' & t2.fname)=16, +
'.....'=5, t2.phone=8 +
FROM sales t1, sales t2 WHERE +
t2.rownum = (t1.rownum + .vcnt / 2 + 1) +
AND t1.rownum <= (.vcnt / 2) +
UNION +
SELECT (t1.lname + ',' & t1.fname)=16, +
'.....'=5, t1.phone=8, ' ', ' ', ' ', ' ' +
FROM sales t1 +
WHERE t1.rownum = (.vcnt / 2 + 1) +
ORDER BY t1.rownum
ENDIF
CLEAR VAR vcnt
Notice how easy it is to add dots to connect the names and telephone
numbers and how easy it is to specify 10 spaces to separate the two
columns. To produce two columns, you simply use two copies of the
table, nicknaming one copy T1 and the other T2.
The first SELECT command works on lists that have an even number of
rows, and the second works on rows that have an odd number of rows.
The second is more complex, using a UNION SELECT to get the last row.
You can make your list look better by using the SET HEADINGS OFF
command to remove the SELECT headings and then using the WRITE command
to add your own headings.