From b939ad368af00f2265a915d259041b8e2cb303c4 Mon Sep 17 00:00:00 2001 From: Ian Jackson Date: Wed, 19 Aug 2009 23:37:10 +0100 Subject: [PATCH] Show medians etc. --- yarrg/web/query_commod | 64 +++++++++++++++++++++++++++++++++--------- 1 file changed, 50 insertions(+), 14 deletions(-) diff --git a/yarrg/web/query_commod b/yarrg/web/query_commod index aeee3ce..9a7d2bc 100644 --- a/yarrg/web/query_commod +++ b/yarrg/web/query_commod @@ -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); -

Offers to <% uc $bs |h %> <% $commodname |h %>

@@ -109,17 +119,43 @@ foreach my $bs (split /_/, $ARGS{BuySell}) { Archipelago Island -Best stall +Unique best stall Best price Qty at best +Median price +Total qty -% my $row; -% while ($row=$sth->fetchrow_hashref) { - <% $row->{'archipelago'} %> - <% $row->{'islandname'} %> - <% $row->{'stallname'} %> - <% $row->{'price'} %> - <% $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'} }; + <% $island->{'archipelago'} |h %> + <% $island->{'islandname'} |h %> + <% $nstalls==1 ? $best->{'stalls'}[0] : "$nstalls offers" |h %> + <% $best->{'price'} %> + <% $best->{'qty'} %> + <% $median %> + <% $tqty %> % } -- 2.30.2