#! /usr/bin/perl

use ccbs;
use strict;
use warnings;

my $cgi = new CGI;
my $id = $cgi->param('id');

my $dbh = ccbs::db_connect();

my $songs = ccbs::db_fetch_all($dbh, 'SELECT machinename,song,title,artist,avg_score,stddev_score,high_score,high_score_player,high_score_nick FROM machinesongs NATURAL JOIN songs NATURAL JOIN machines NATURAL LEFT JOIN ( SELECT song,AVG(score)::integer AS avg_score,STDDEV(score)::integer AS stddev_score,MAX(score) AS high_score FROM scores GROUP BY song ) t1 NATURAL LEFT JOIN ( SELECT DISTINCT ON (song) song,player AS high_score_player,nick AS high_score_nick,score AS high_score FROM scores NATURAL JOIN players ORDER BY song,score DESC ) t2 ORDER BY machine,LOWER(title)');

ccbs::print_header();
ccbs::process_template('songs.tmpl', 'Songs', {
	songs => $songs,
});
$dbh->disconnect;
