http://www.perlmonks.org?node_id=11106471

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

I have strange problem.

I have Pg database table with 20,000+ rows. The only important column from this table is the timestamp column called date.

I need to get all unique dates from the table, so I run the following code in the terminal:

mydb=# select distinct cast(date as date) from myschema.mytable; -[ RECORD 1 ]----------- date | 2019-09-18 -[ RECORD 2 ]----------- date | 2019-09-20 -[ RECORD 3 ]----------- date | 2019-09-19

… which is correct. However, when I run the following code, it output three times the same date:

$sth_dates_uniq = $dbh->prepare("select distinct cast(date as date) fr +om myschema.mytable;") or die; $sth_dates_uniq->execute(); my @all_rows; while ( my $ref = $sth_dates_uniq->fetchrow_arrayref()) { push(@all_rows, $ref); } foreach my $row_ref (@all_rows) { print "@{$row_ref}\n"; } # Output 2019-09-19 2019-09-19 2019-09-19

Do I do something wrong or is this a bug? What else could I do? I could get all the data from the table, sort them and get all unique values, however, the table is quite big to do so. I’d like to have it as fast as possible.