chiark / gitweb /
Show medians etc.
[ypp-sc-tools.db-live.git] / yarrg / web / query_commod
index aeee3cee83d0bf8288ee79903ec005e20b36f4fc..9a7d2bc36dc7bad67e67bfbc6c92688e6902862a 100644 (file)
@@ -91,16 +91,26 @@ $someresults->();
 foreach my $bs (split /_/, $ARGS{BuySell}) {
        $bs =~ m/^(buy|sell)$/ or die;
        $bs= $1;
+       my ($ascdesc) = ($bs eq 'buy')
+               ? ('DESC')
+               : ('ASC');
+
+       my $islands= $dbh->prepare(
+       "SELECT islandid, archipelago, islandname, sum(qty) as tqty
+               FROM $bs NATURAL JOIN islands
+               WHERE commodid = ?
+               GROUP BY islandid,
+               ORDER BY archipelago, islandname"
+               );
 
-       my $sth= $dbh->prepare(
-       "SELECT archipelago, islandname, stallname, price, qty
-               FROM $bs NATURAL JOIN stalls NATURAL JOIN islands
-               WHERE commodid = ?"
+       my $offers= $dbh->prepare(
+       "SELECT stallname, price, qty
+               FROM $bs NATURAL JOIN stalls
+               WHERE commodid = ? AND islandid = ?
+               ORDER BY price $ascdesc"
                );
        # fixme this query is utterly wrong
 
-       $sth->execute($commodid);
-
 </%perl>
 
 <h2>Offers to <% uc $bs |h %> <% $commodname |h %></h2>
@@ -109,17 +119,43 @@ foreach my $bs (split /_/, $ARGS{BuySell}) {
 <tr>
 <th>Archipelago
 <th>Island
-<th>Best stall
+<th>Unique best stall
 <th>Best price
 <th>Qty at best
+<th>Median price
+<th>Total qty
 </tr>
-%      my $row;
-%      while ($row=$sth->fetchrow_hashref) {
-<tr> <td><% $row->{'archipelago'} %>
-     <td><% $row->{'islandname'} %>
-     <td><% $row->{'stallname'} %>
-     <td><% $row->{'price'} %>
-     <td><% $row->{'qty'} %>
+%      $islands->execute($commodid);
+%      my $island;
+%      while ($island= $islands->fetchrow_hashref) {
+%              my $islandid= $island->{'islandid'};
+%              $offers->execute($commodid, $islandid);
+%              my ($offer, $best, $median);
+%              my $tqty= $island->{'tqty'};
+%              my $cqty= 0;
+%              while ($offer= $offers->fetchrow_hashref) {
+%                      if (!$best) {
+%                              $best= { 'price' => $offer->{'price'} };
+%                      }
+%                      if ($offer->{'price'} == $best->{'price'}) {
+%                              $best->{'qty'} += $offer->{'qty'};
+%                              push @{ $best->{'stalls'} },
+%                                      $offer->{'stallname'};
+%                      }
+%                      $cqty += $offer->{'qty'};
+%                      if ($cqty*2 >= $tqty && !defined $median) {
+%                              $median= $offer->{'price'};
+%                      }
+%              }
+%              $cqty == $tqty or die "$bs $cqty $tqty $commodid $islandid ";
+%              my $nstalls= @{ $best->{'stalls'} };
+<tr> <td><% $island->{'archipelago'} |h %>
+     <td><% $island->{'islandname'} |h %>
+     <td><% $nstalls==1 ? $best->{'stalls'}[0] : "$nstalls offers" |h %>
+     <td><% $best->{'price'} %>
+     <td><% $best->{'qty'} %>
+     <td><% $median %>
+     <td><% $tqty %>
 </tr>
 %      }
 </table>