Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Error in query with DBD-SQLite-1.37 - Aggregate functions are not allowed in the GROUP BY clause.

by saumitra121 (Novice)
on Jul 30, 2012 at 09:50 UTC ( [id://984410]=perlquestion: print w/replies, xml ) Need Help??

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

Monks,

I have a query below which is working fine in SQLite with DBD-SQLite-1.14 but when I am upgrading to DBD-SQLite-1.37 it is giving following error:

DBD::SQLite::db selectall_arrayref failed: aggregate functions are not + allowed in the GROUP BY clause
I have been searching for last two days why upgrading the DBD-SQLite is causing this query to fail.

The query is as below:

SELECT stat_day, obs_day, stat_week, obs_week, NULL AS stat_cputotal, +stat_memfree, stat_md5, stat_month, obs_month, stat_type, stat_memuse +d, stat_10min, obs_10min, NULL AS stat_cpusystem, stat_epoch, obs_epo +ch, stat_30day, obs_30day, stat_idx, obs_idx, NULL AS stat_cpuidle, s +tat_memtotal, stat_qtr, obs_qtr, NULL AS stat_cpuuser, stat_hour, obs +_hour, obs_10min, stat_10min, obs_30day, stat_30day, obs_day, stat_da +y, obs_epoch, stat_epoch, obs_hour, stat_hour, obs_idx, stat_idx, obs +_month, stat_month, obs_qtr, stat_qtr, obs_week, stat_week, serial_nu +m, NULL AS dputil_cpuid, NULL AS wms_pk FROM ((SELECT stat_qtr, obs_q +tr, stat_qtr AS cus_qtr, stat_qtr AS crash_qtr, stat_qtr AS evt_qtr, +stat_30day, obs_30day, stat_30day AS cus_30day, stat_30day AS crash_3 +0day, stat_30day AS evt_30day, stat_month, obs_month, stat_month AS c +us_month, stat_month AS crash_month, stat_month AS evt_month, stat_we +ek, obs_week, stat_week AS cus_week, stat_week AS crash_week, stat_we +ek AS evt_week, stat_day, obs_day, stat_day AS cus_day, stat_day AS c +rash_day, stat_day AS evt_day, stat_hour, obs_hour, stat_hour AS cus_ +hour, stat_hour AS crash_hour, stat_hour AS evt_hour, stat_10min, obs +_10min, stat_10min AS cus_10min, stat_10min AS crash_10min, stat_10mi +n AS evt_10min, stat_epoch, obs_epoch, stat_epoch AS cus_epoch, stat_ +epoch AS crash_epoch, stat_epoch AS evt_epoch, stat_idx, obs_idx, sta +t_idx AS cus_idx, stat_idx AS crash_idx, stat_idx AS evt_idx, obs_10m +in, obs_30day, obs_day, obs_epoch, obs_hour, obs_idx, obs_month, obs_ +qtr, obs_week, serial_num, stat_md5, MIN(stat_memfree) AS stat_memfre +e, MIN(stat_md5) AS stat_md5, MIN(stat_type) AS stat_type, MIN(stat_m +emused) AS stat_memused, MIN(stat_memtotal) AS stat_memtotal FROM (se +lect * from ( mem_stat NATURAL LEFT OUTER JOIN host )) GROUP BY stat_ +qtr, stat_30day, stat_month, stat_week, stat_day, stat_hour, stat_10m +in, stat_epoch, stat_idx, obs_10min, obs_30day, obs_day, obs_epoch, o +bs_hour, obs_idx, obs_month, obs_qtr, obs_week, serial_num, stat_md5) +)
I am using DBI-1.609 and SQLite v3.7.11.

Updating the question with new findings:

After installing SQLite v3.7.11, I tried reinstalling DBD-SQLite-1.14 from source. While running "perl Makefile.PL", it gave following message:
Checking installed SQLite version... SQLite version must be at least 3.3.9. No header file at that version or higher was found. Using the local version instead. Checking if your kit is complete... Looks good Multiple copies of Driver.xst found in: /usr/lib64/perl5/site_perl/5.8 +.8/x86_64-linux-thread-multi/auto/DBI/ /usr/lib64/perl5/vendor_perl/5 +.8.8/x86_64-linux-thread-multi/auto/DBI/ at Makefile.PL line 140 Using DBI 1.609 (for perl 5.008008 on x86_64-linux-thread-multi) insta +lled in /usr/lib64/perl5/site_perl/5.8.8/x86_64-linux-thread-multi/au +to/DBI/ Writing Makefile for DBD::SQLite
I went on and installed the DBD-SQLite-1.14. Then I ran the following code to check which sqlite version is being used by DBD-SQLite.
$dbh = DBI->connect("dbi:SQLite:dbname=myDB", "", ""); print $dbh->{sqlite_version};
And it showed version as 3.4.2

Why is it unable to detect the current sqlite version?

When I tried installing the DBD-SQLite-1.37, it detected the SQLite version successfully.
  • Comment on Error in query with DBD-SQLite-1.37 - Aggregate functions are not allowed in the GROUP BY clause.
  • Select or Download Code

Replies are listed 'Best First'.
Re: Error in query with DBD-SQLite-1.37 - Aggregate functions are not allowed in the GROUP BY clause.
by roboticus (Chancellor) on Jul 30, 2012 at 12:38 UTC

    saumitra121:

    If you slightly organize your SQL, it looks rather peculiar to me:

    SELECT stat_day, obs_day, stat_week, obs_week, NULL AS stat_cputotal, + stat_memfree, stat_md5, stat_month, obs_month, stat_type, + stat_memused, stat_10min, obs_10min, NULL AS stat_cpusystem, + stat_epoch, obs_epoch, stat_30day, obs_30day, stat_idx, obs_id +x, NULL AS stat_cpuidle, stat_memtotal, stat_qtr, obs_qtr, + NULL AS stat_cpuuser, stat_hour, obs_hour, obs_10min, stat_10m +in, obs_30day, stat_30day, obs_day, stat_day, obs_epoch, stat_epoc +h, obs_hour, stat_hour, obs_idx, stat_idx, obs_month, stat_month, + obs_qtr, stat_qtr, obs_week, stat_week, serial_num, + NULL AS dputil_cpuid, NULL AS wms_pk + FROM (( SELECT stat_qtr, obs_qtr, stat_qtr AS cus_qtr, stat_qtr AS crash_ +qtr, stat_qtr AS evt_qtr, stat_30day, obs_30day, stat_30day AS +cus_30day, stat_30day AS crash_30day, stat_30day AS evt_30day, stat_m +onth, obs_month, stat_month AS cus_month, stat_month AS crash_mo +nth, stat_month AS evt_month, stat_week, obs_week, stat_week AS + cus_week, stat_week AS crash_week, stat_week AS evt_week, stat_day, +obs_day, stat_day AS cus_day, stat_day AS crash_day, stat_day AS ev +t_day, stat_hour, obs_hour, stat_hour AS cus_hour, stat_hour AS c +rash_hour, stat_hour AS evt_hour, stat_10min, obs_10min, stat_10min A +S cus_10min, stat_10min AS crash_10min, stat_10min AS evt_10min, stat_e +poch, obs_epoch, stat_epoch AS cus_epoch, stat_epoch AS crash_ep +och, stat_epoch AS evt_epoch, stat_idx, obs_idx, stat_idx AS cu +s_idx, stat_idx AS crash_idx, stat_idx AS evt_idx, obs_10min, obs +_30day, obs_day, obs_epoch, obs_hour, obs_idx, obs_month, obs_qtr, + obs_week, serial_num, stat_md5, MIN(stat_memfree) AS stat_memfree, + MIN(stat_md5) AS stat_md5, MIN(stat_type) AS stat_type, + MIN(stat_memused) AS stat_memused, MIN(stat_memtotal) AS s +tat_memtotal FROM ( select * from (mem_stat NATURAL LEFT OUTER JOIN host ) + ) GROUP BY stat_qtr, stat_30day, stat_month, stat_week, stat_day, st +at_hour, stat_10min, stat_epoch, stat_idx, obs_10min, obs_30day, o +bs_day, obs_epoch, obs_hour, obs_idx, obs_month, obs_qtr, obs_wee +k, serial_num, stat_md5 ))

    In general, your use of parenthesis looks odd, but the statement that looks off is:

    select * from (mem_stat NATURAL LEFT OUTER JOIN host )

    I'd leave the parenthesis off this one. In general, to debug something ugly like this, I like to start from the inside out and verify the sql. You don't need all the columns and such.

    So first, I'd try in the sqlite3 shell:

    select * from (mem_stat NATURAL LEFT OUTER JOIN host )

    If that works, then I'd go with:

    SELECT stat_qtr, obs_qtr, stat_qtr AS cus_qtr, stat_qtr AS crash_qtr, MIN(stat_memfree) AS stat_memfree FROM ( <<<previous statement after it works>>> )

    (As I said, for debugging you don't need to have all columns, at least at first.) Next, I would try:

    <<<previous statement after it works>>> GROUP BY stat_qtr

    Then, piece by piece, rebuild your statement. When it fails, you know which small piece to look at. Fix that piece and continue until you've rebuilt the statement. Generally, I add most of the columns toward the end, as I find that columns are the things I rarely have trouble with.

    If I had to guess, your odd use of parenthesis is the problem.

    ...roboticus

    When your only tool is a hammer, all problems look like your thumb.

      Notably:

      MIN(stat_md5) AS stat_md5, ... GROUP BY ... stat_md5

      Which exactly matches the error message the original poster is getting. (It also looks like an error in another way: Just before that in the select statement is a request for stat_md5 as a separate field. So he's asking for two fields, with the same name, one of which is an aggregate of the first.)

Re: Error in query with DBD-SQLite-1.37 - Aggregate functions are not allowed in the GROUP BY clause.
by Anonymous Monk on Jul 30, 2012 at 10:03 UTC
    does it work from commandline sqlite3 client?
      No its giving the same error when I ran it on SQLite-3.7.11 command line. Please check my updated answer with new findings and guide me with troubleshooting this issue.

        If it does not work on the sqlite3 command line then it is a problem with SQL and SQLite. There is very little we can do from a Perl perspective.

        As your SQL statement is quite long, try reducing the columns and aggregates.

        The error message says that you try to GROUP BY something that is an aggregate function, which SQLite does not allow (and which does not make much sense to me either). Consider moving your subselects into views maybe to disambiguate the aggregates etc.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://984410]
Approved by kcott
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others chilling in the Monastery: (2)
As of 2024-04-26 00:47 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found