Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??

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. Id like to have it as fast as possible.

In reply to DBI: select distinct returns same values by tukusejssirs

Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":

  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?

    What's my password?
    Create A New User
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others having an uproarious good time at the Monastery: (4)
    As of 2021-04-17 23:50 GMT
    Find Nodes?
      Voting Booth?

      No recent polls found