MegaVoice has asked for the wisdom of the Perl Monks concerning the following question:

Hello,
I am attempting to re-create a pivot table into a spreadsheet based on the results that I pull from 3 separate data queries. Each query would return to results to put together into a pivot table. For example:
Query 1:
7/20/2007   25 30 18 10
7/21/2007   15 8 23 14
Query 2:
7/20/2007   10 11 44 23
7/21/2007   32 28 17 11
Query 3:
7/20/2007   33 22 11 24
7/21/2007   14 24 33 40

The final result should look like:
7/20/2007   25 30 18 10
                   10 11 44 23
                   14 24 33 40

I don't know of a way to create a pivot table in MS-SQL 2000 (the new pivot command is available in 2005). There was nothing in the Spreadsheet::WriteExcel either.

I was wondering if using the selectall_hashref within DBI would be an option and then merge the hashes since the keys would line up. I am not proficient with hashes yet and haven't been able to find any good examples of the selectall_hashref online. In addition, the examples that I did find caused me confusion on how to pull the data back. I could create the hash reference, and found a way to get the keys printed out by using a loop with keys %{$hash_ref}, but did not know how to get the data back. I wasn't sure if merging the various hashes together would work either. I was thinking of putting data into 3 separate arrays and the manipulating them to get the data formatted out that I need.

I know this sounds a little confusing, but I was hoping that there might be something that I am missing in my thought process.

Here is the line that I am using to pull the data:
my $hash_ref = $dbh->selectall_hashref($sql,$key_field,{},@values);

I can print the keys with the following:
for my $id (keys %{$hash_ref}) { print "ID => $id\n"; }

Thank you in advance!

20070904 Janitored by Corion: Added formatting, as per Writeup Formatting Tips

Replies are listed 'Best First'.
Re: Merging Results from 3 different queries
by spatterson (Pilgrim) on Jul 24, 2007 at 16:06 UTC
      Unfortunately, I am having to use a stored procedure in the database to pivot the data just to get a single result set. I am unable to use a join inside the call to the stored procedure to return 3 the pivots that I need.
Re: Merging Results from 3 different queries
by BrowserUk (Patriarch) on Jul 24, 2007 at 16:45 UTC

    If you formatted your post, you'd probably get more responses.

    Assuming that each of your queries produces a hashref containing the same keys, then printing the values as you've asked might look something like this:

    my $hash_ref1 = $dbh->selectall_hashref( $sql1, $key_field1, {}, @valu +es1 ); my $hash_ref2 = $dbh->selectall_hashref( $sql2, $key_field2, {}, @valu +es2 ); my $hash_ref3 = $dbh->selectall_hashref( $sql3, $key_field3, {}, @valu +es3 ); ## for each id for my $id ( keys %{ $hashref1 } ) { ## extract an array of results for that key from each hash my @results1 = @{ $hashref1->{ $id } }; my @results2 = @{ $hashref2->{ $id } }; my @results3 = @{ $hashref3->{ $id } }; ## join them together with spaces ## and print them along with the key printf "%14s : %s \t%s\t%s\n", $id, join( ' ', @results1 ), join( ' ', @results2 ), join( ' ', @results3 ); }

    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.
      I took your code and pulled into my script. However, I was getting an error:

      Not an ARRAY reference

      I modified the code as follows:
      for my $id ( sort keys %{ $hashref1 } ) { ## extract an array of results for that key from each hash my %results1 = %{ $hashref1->{ $id } }; my %results2 = %{ $hashref2->{ $id } }; my %results3 = %{ $hashref3->{ $id } }; ## join them together with spaces ## and print them along with the key printf "%14s : %s \t%s\t%s\n", $id, join( ' ', %results1 ), join( ' ', %results2 ), join( ' ', %results3 ); }

      I was then able to get the data back out of the hash but it did not maintain the data order (columns) that I get when running the straight SQL. Perhaps using the selectall_hashref may not be the answer.

      If people have other suggestions for options, feel free to pass them along.