OLAP is nothing but an SQL aggregate with a big "GROUP BY" that you then display in a matrix of columns and rows. I prefer having the option to query the live data. If you want to boost performance by distilling the data, do so as a nightly batch job that stores the aggregate query results into a heavily indexed "temp" table that avoids the bulk of the joins and/or aggregation. That's just a preference though.

What follows is some proof of concept stuff I did. I pulled it from the larger project, so you will need to fill in some bits and pieces like setting up $dbh via DBI, HTML head/body codes, and spitting out HTML headers.

It was written to use PostgreSQL, but the SQL is generic enough.

Change @rowhead, @colheads, @datacols, $cubetable to match your system.

NOTE: There is much room for improvement, but the basic concept is there. Much convolution is due to my need to generate the HTML table in proper order. In retrospect there is probably a much more sane way to code it. . .

use strict; use DBI; my $dbh; #SET THIS TO A VALID DBI CONNECTION HANDLE. my @rowheads = qw/fiscal_year sale_in_territory/; my @colheads = qw/class product_id/; my @datacols = qw/units_sold/; my $cubetable = 'transactions'; my @pivots = @rowheads; push @pivots, @colheads; my (%values,%names,%ordinals,%filtered,%totals,%columnheaders); my $sth = $dbh->prepare('select '.join(',',@rowheads).','.join(',',@colheads).','.join(',',@datacols). +' from '.$cubetable.' ORDER BY '.join(',',@pivots)) || die $dbh->errstr; $sth->execute || die $dbh->errstr; my @row; while (@row = $sth->fetchrow_array) { #put the data values in a safe place my @data; for (my $i = 0; $i < @datacols; $i++) { my $d = pop @row; push @data,$d; } my $continue = 0; foreach (@data) { $continue = 1 if defined $_; } next unless $continue; my @key = @{GetKey(@row)}; my $key = join(' ',@key); if (defined $values{$key}) { my $count = -1; foreach (@{$values{$key}}) { $_ = $_ + $data[$count++]; } } else { $values{$key} = \@data; } #now set up the column header hash splice(@key,0,(@rowheads)); $key = join(' ',@key); if (defined $columnheaders{$key}) { my $count = -1; foreach(@{$columnheaders{$key}}) { $_ = $_ + $data[$count++]; } } else { $columnheaders{$key} = \@data; } } #build the column headers in a 2D array #my $colcount = 1; #foreach (@colheads) { # $colcount = $colcount * (@{$names{$_}}); #} #$colcount = $colcount * (@datacols); my $stop; my @colkey; my $count = 0; foreach (@colheads) { $colkey[$count] = 0; $stop = $stop.$#{$names{$colheads[$count]}}.' '; $count++; } chop $stop; my @cols; my $count = 0; while ($stop ne join(' ',@colkey)) { CalcCol($#colheads); my $key = join(' ',@colkey); # next if !defined $columnheaders{$key}; $cols[$count] = $key; $count++; } #print '<pre>',main::Dumper(@cols),'</pre>'; #display print '<table border = 1>'; my $datacount; #print the column headers my $count = 0; my @colkeys; foreach my $h (@colheads) { $datacount = 0; print '<tr>'; print '<td colspan="'.(@rowheads-1).'"></td>'; print "<td>$h</td>"; foreach my $key (@cols) { next if !defined $columnheaders{$key}; $datacount++; my @tmp = split(' ', $key); foreach (@datacols) { print '<td>'.${$names{$h}}[$tmp[$count]].'</td>'; } } print '</tr>'; $count++; } #print the row heads & data col heads print '<tr>'; print '<td>',join('</td><td>',@rowheads),'</td>'; for (my $i = 0; $i < $datacount;$i++) { foreach my $h (@datacols) { print "<td>$h</td>"; } } print '</tr>'; #print the row headers and data; my @rowheader; my @rowkey; my $rowcount; my $count = 0; my $stop = ''; foreach (@rowheads) { $rowkey[$count] = 0; $stop = $stop.$#{$names{$rowheads[$count]}}.' '; $count++; } chop $stop; my $first = 1; while (join(' ',@rowkey) ne $stop) { CalcRow($#rowheads); my $rowOK = 0; my $count = 0; foreach my $h (@rowheads) { $rowheader[$count] = ${$names{$h}}[$rowkey[$count]]; $count++; } my $rowstr = '<tr><td>'.join('</td><td>',@rowheader).'</td>'; foreach my $k (@cols) { next if !defined $columnheaders{$k}; my $data = $values{join(' ',@rowkey).' '.$k}; my @data; if (defined($data)) { @data = @{$data}; $rowOK = 1; } else { foreach (@datacols) { push @data, undef; } } $rowstr = $rowstr.'<td>'.join('</td><td>',@data).'</td>'; } if ($rowOK) { print $rowstr,'</tr>'; } } sub CalcRow { my $pos = shift; if ($pos == 0) { $rowkey[0]++ if $rowkey[0] <= $#{$names{$rowheads[0]}}; return 0; } else { if ($rowkey[$pos] >= $#{$names{$rowheads[$pos]}}) { #PRINT TOTAL LINE HERE $rowkey[$pos] = 0; CalcRow($pos-1); } else { $rowkey[$pos]++; return 0; } } return 1; } sub CalcCol { my $pos = shift; if ($pos == 0) { $colkey[0]++ if $colkey[0] <= $#{$names{$colheads[0]}}; return 0; } else { if ($colkey[$pos] >= $#{$names{$colheads[$pos]}}) { #PRINT TOTAL LINE HERE $colkey[$pos] = 0; CalcCol($pos-1); } else { $colkey[$pos]++; return 0; } } return 1; } print '</table>'; #print '<p>columnheaders:<br><pre>',main::Dumper(%columnheaders),'</pr +e><p>'; #print '<p>stop: ',main::Dumper($stop); #print '<p>stop: ',main::Dumper(join(' ',@rowkey)); #print '<p>NAMES: ',main::Dumper(%names),'<p>'; #print 'VALUES: ',main::Dumper(%values),'<p>'; #print 'ORDINALS: ',main::Dumper(%ordinals),'<p>'; sub GetKey { my $count = 0; my @key; foreach my $val (@_) { if (defined $ordinals{$pivots[$count].$val}) { push @key, $ordinals{$pivots[$count].$val}; } else { my $o = push @{$names{$pivots[$count]}},$val; $o--; $ordinals{$pivots[$count].$val} = $o; push @key, $o; } $count++; } return \@key; } 1;
Questions, comments to rob AHT cabrion DHOT com.

I really would like to know if someone decides to hack on this, or builds a module from it.


In reply to Re: Re: Microsoft Analysis Server - Data Cubes by Anonymous Monk
in thread Microsoft Analysis Server - Data Cubes by gary kuipers

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.