#!/usr/bin/perl -w use strict; # use diagnostics; use DBI; use POSIX; use Data::Dumper; my $station = shift @ARGV or die; my $db = DBI->connect("dbi:SQLite:dbname=rivers.db") or die $DBI::errstr; my $years_st = $db->prepare("select distinct substr(timestamp, 1, 4) from levels where stationid=?"); my $getdata_st = $db->prepare("select strftime('2000-%m-%dT%H:%M:%S.000Z', timestamp) as t, level from levels where stationid = ? and timestamp like ? || '-%'"); $years_st->execute($station); my @years = @{$years_st->fetchall_arrayref}; my %data; foreach my $y_ref (@years) { my $y = $y_ref->[0]; $getdata_st->execute($station, $y); while(my $row = $getdata_st->fetchrow_arrayref) { $data{$row->[0]}{$y} = $row->[1]; } } # print Dumper(\%data); foreach my $ts (sort keys %data) { print "$ts\t"; foreach my $y_ref (@years) { my $y = $y_ref->[0]; print $data{$ts}{$y} || '-', "\t"; } print "\n"; }