PIVOTS

Rather than replicating all of a relational database in a spreadsheet,
AnalyRIM incorporates an actual relational DBMS which is designed to be
able to handle large amounts of data and do reports as needed. It has
become common to treat a spreadsheet as a relation, because they look rather
similar, but actual usage of relations often requires manipulations which are
more than one usually gets in a spreadsheet alone.

To use the DBMS, you need to define the relation(s) you will be using, and
then add your data into them from the spreadsheet. Then you may use simple
commands to extract what you want, and either just print the results or
copy results back into the spreadsheet somewhere.

Let us suppose we have a region in the spreadsheet that looks like

   Region     Salesp   Amount   Date
   NE         Fred     232.21   02-feb-2016
   NE         Mary     934.22   15-feb-2016
   N          George  1227.32   12-feb-2016
   E          Fred     942.22   21-feb-2016
   S          Mary    1604.24   24-feb-2016
   NE         John     465.63   25-feb-2016
   E          George  1657.66   27-feb-2016
   N          Fred     237.77   03-mar-2016

This may go much longer for a real case, but suppose we want to get
totals of orders per region, or per owner? Or perhaps by month?

To handle this, define a DBMS relation first, so it can be populated from
the spreadsheet data.

CMDRIM
RIM>DEFINE sales
D>owner sale
D>attributes
D>region text var key
D>salesp text var key
D>amount real 
D>date text var key
D>relations
D>allsales WITH region salesp amount date
D>end
RIM>open sales
RIM>user sale
RIM>exit

Load the database from cells. 
Let us suppose cells A1...D20 are the material.

CMDOPEN sales
CMDUSER sale
CMDGETATT allsales,a1:d1
CMDLOD A1,A2:D20
   *C title cell is A1 here, has labels
   *C this loads the data. Now we can select data
CMDFIND allsales
CMDWHERE A1:A2,EQ
CMDGET A1,A200:D200
*C above puts rows into spreadsheet cells A22 thru D200
*C following just passes compute operation to DBMS. Do this once per
*C region, for example.
CMDSAV S40:V220
CMDIRIM COMPUTE SUM amount FROM allsales WHERE region EQS NE
CMDIRIM COMPUTE SUM amount FROM allsales WHERE region EQS N
CMDIRIM COMPUTE SUM amount FROM allsales WHERE region EQS E
CMDIRIM COMPUTE SUM amount FROM allsales WHERE region EQS S
CMDIRIM COMPUTE SUM amount FROM allsales WHERE region EQS W
CMDCLOSE

Unfortunately there is no FOREACH operation in RIM so it seems having
some of that ability in the spreadsheet is desirable. RIM can select
data and get sum, count, average, max or min, but some operations seem
complex even with join, union, intersection, and projection all
available. It looks like more relations, sorting, and removing
duplicates might be needed, and Analyrim needs to be used to loop
over a set of inputs.


