Grouping by Category
The following output is a cross-tabular report that groups the products by price range. This grouping cannot be done by using a SQL GROUP BY clause. Moreover, to process the records in order of price category, the program would have to sort the table by price. The sample program shows how to do it without sorting the data.
The sample program uses
an SQR EVALUATE command to determine the price category and assign
the array subscript #i
to 0, 1, or 2. Then it adds the order quantity to the
array cell that corresponds to the price category (row) and the month
(column).
Program ex8b.sqr
#define max_categories 3
begin-setup
create-array
name=order_qty size={max_categories}
field=category:char field=month_qty:number:3
end-setup
begin-program
do select_data
do print_array
end-program
begin-procedure print_array
let #i = 0
while #i < {max_categories}
let $category = order_qty.category(#i)
let #jan = order_qty.month_qty(#i,0)
let #feb = order_qty.month_qty(#i,1)
let #mar = order_qty.month_qty(#i,2)
let #category_tot = #jan + #feb + #mar
print $category (,1,31)
print #jan (,32,9) edit 9,999,999
print #feb (,42,9) edit 9,999,999
print #mar (,52,9) edit 9,999,999
print #category_tot (,62,9) edit 9,999,999
position (+1)
let #jan_total = #jan_total + #jan
let #feb_total = #feb_total + #feb
let #mar_total = #mar_total + #mar
let #i = #i + 1
end-while
let #grand_total = #jan_total + #feb_total + #mar_total
print 'Totals' (+2,1)
print #jan_total (,32,9) edit 9,999,999
print #feb_total (,42,9) edit 9,999,999
print #mar_total (,52,9) edit 9,999,999
print #grand_total (,62,9) edit 9,999,999
end-procedure print_array
begin-procedure select_data
let order_qty.category(0) = '$0-$4.99'
let order_qty.category(1) = '$5.00-$100.00'
let order_qty.category(2) = 'Over $100'
begin-select
order_date
! the price / price category for the order
c.price &price
move &price to #price_num
evaluate #price_num
when < 5.0
let #i = 0
break
when <= 100.0
let #i = 1
break
when-other
let #i = 2
break
end-evaluate
! The quantity for this order
quantity
let #j = to_number(datetostr(&order_date,'MM')) - 1
if #j < 3
let order_qty.month_qty(#i,#j) =
order_qty.month_qty(#i,#j) + &quantity
end-if
from orders a, ordlines b, products c
where a.order_num = b.order_num
and b.product_code = c.product_code
end-select
end-procedure ! select_databegin-heading 5
print $current-date (1,1)
page-number (1,64) 'Page '
print 'Order Quantity by Product Price Category by Month' (2,11)
print 'Product Price Category' (4,1)
print ' January' (,32)
print ' February' (,42)
print ' March' (,52)
print ' Total' (,62)
print '-' (5,1,70) Fill
end-heading
The following is the output for program ex8b.sqr:
11-JUN-04 Page 1
Order Quantity by Product Price Category by Month
Product Price Category January February March Total
-----------------------------------------------------------------
0-4.99 28 45 12 85
5.00-100.00 25 28 138 191
Over 100 7 9 0 16
Totals 60 82 150 292