Does SELECT DISTINCT work with Perl's DBD::CSV?

爱⌒轻易说出口 提交于 2019-12-10 15:19:10

问题


I found a SELECT-example on the web. When I try it in my script I get this error-message:

Specifying DISTINCT when using aggregate functions isn't reasonable - ignored. at /usr/lib/perl5/site_perl/5.10.0/SQL/Parser.pm line 496.

#!/usr/bin/perl
use warnings;
use strict;
use DBI;

my $dbh = DBI->connect( "DBI:CSV:", undef, undef, { RaiseError => 1, AutoCommit => 1 } );
my $table = 'artikel';

my $array_ref = [   [ 'a_nr', 'a_name', 'a_preis' ],  
            [ 12, 'Oberhemd', 39.80, ],
            [ 22, 'Mantel', 360.00, ],
            [ 11, 'Oberhemd', 44.20, ],
            [ 13, 'Hose', 119.50, ],
        ];
$dbh->do( "CREATE TEMP TABLE $table AS IMPORT(?)", {}, $array_ref );

my $sth = $dbh->prepare( "SELECT DISTINCT a_name FROM $table" );
$sth->execute();
$sth->dump_results();
$dbh->disconnect();

Does SELECT DISTINCT not work with DBD::CSV or is something wrong with my script?

edit: The output is

'Oberhemd' 'Mantel' 'Oberhemd' 'Hose' 4 rows

I thought it should be

'Oberhemd' 'Mantel' 'Hose' 3 rows

Installed versions:

Perl : 5.010000 (x86_64-linux-thread-multi) OS : linux (2.6.31) DBI : 1.609 DBD::Sponge : 12.010002 DBD::SQLite : 1.25 DBD::Proxy : 0.2004 DBD::Gofer : 0.011565 DBD::File : 0.37 DBD::ExampleP : 12.010007 DBD::DBM : 0.03 DBD::CSV : 0.26


回答1:


Hi This is an easily reproducable bug. SELECT data_display_mask FROM test.csv returns 200 plus rows. SELECT DISTINCT data_display_mask FROM test.csv returns the warning message and same 200 rows.

If i do an awk, sort -u for unique ( values of the row ) I get 36 values, which is what I would expect.

Certainly a bug in the code.

-Kanwar

perl -V Summary of my perl5 (revision 5 version 10 subversion 0) configuration: Platform: osname=linux, osvers=2.2.24-6.2.3, archname=i686-linux-thread-multi

DBD::CSV 0.26 SQL::Parser 1.23 DBI 1.609

example:

Specifying DISTINCT when using aggregate functions isn't reasonable - ignored. at /opt/perl2exe/perl5/lib/site_perl/5.10.0/SQL/Parser.pm line 496. 87060 87060 87060 87060

SQL used is SELECT DISTINCT entry_id FROM test.csv




回答2:


Note that the message about something being not reasonable is

  1. Only a warning. Your script works nevertheless.
  2. Confusing and non-sensical: you don't use any aggregate functions.

I smell a bug in either DBD::CSV or SQL::Statement.

Edit: DISTINCT is explicitly allowed in SQL::Statement




回答3:


my $sth = $dbh->prepare("SELECT DISTINCT $attributeName1, COUNT( $attributeName2) FROM tableName GROUP BY $attributeName1, $attributeName2");

this gave me: attributeName1 and a distinct count of attributeName2




回答4:


This is an example of a more general phenomenon with DBD::CSV, namely, that it allows a lot of SQL syntax for which the meaning is silently ignored.

I have seen cases of SELECT DISTINCT that actually filter out duplicates, so the case you mention here seems a bug, but I haven't found a way to make the DISTINCT in SELECT COUNT(DISTINCT foo) FROM bar do anything.




回答5:


Works for me. I get 3 rows back.

    $ perl x.pl
'Oberhemd'
'Mantel'
'Hose'
3 rows

perl -MDBI -le 'DBI->installed_versions;'
      Perl            : 5.010001    (i686-linux-gnu-thread-multi)
      OS              : linux   (2.6.24-28-server)
      DBI             : 1.617
      DBD::mysql      : 4.020
      DBD::Sys        : 0.102
      DBD::Sponge     : 12.010002
      DBD::SQLite     : 1.33
      DBD::Proxy      : 0.2004
      DBD::Pg         : 2.17.2
      DBD::Oracle     : 1.38
      DBD::ODBC       : 1.33
      DBD::Multiplex  : 2.014122
      DBD::Gofer      : 0.015057
      DBD::File       : 0.40
      DBD::ExampleP   : 12.014310
      DBD::DBM        : 0.06
      DBD::CSV        : 0.30

Added:

perl -MSQL::Statement -le 'print $SQL::Statement::VERSION'
1.31

Version 1.23, release November 20th, 2009 * Correct handling of DISTINCT in aggregate functions




回答6:


I met the same problem.

You can turn around this problem using a GROUP BY statement instead of DISTINCT.

This is just a turn around waiting for the resolution of a bug ...



来源:https://stackoverflow.com/questions/1941321/does-select-distinct-work-with-perls-dbdcsv

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!