[MonetDB-users] Aggregating by a computed field?
Hello, I recently install MonetDB and have been trying to get a feel for its speed and storage. I ran into an issue with SELECT queries trying to GROUP BY a computed field. For example, imagine a table like this: CREATE TABLE foo ( ts TIMESTAMP NOT NULL, category VARCHAR(32) NOT NULL ); I want to count the number of categories by year, so I want to craft a query like this: sql> SELECT EXTRACT( year from ts ) as year, category, COUNT(category) FROM foo GROUP BY 1, 2 ORDER BY 3 DESC; syntax error, unexpected sqlINT in: "SELECT EXTRACT( year from ts ) as year, category, COUNT(category) FROM foo GROUP BY 1, 2 ORDER BY 3 DESC" Unfortunately, MonetDB does not appear to support positional fields in the GROUP BY clause. So I tried rewriting it like this: sql> SELECT EXTRACT( year from ts ) as year, category, COUNT(category) FROM foo GROUP BY EXTRACT( year from ts ), category ORDER BY 3 DESC; syntax error, unexpected EXTRACT in: "SELECT EXTRACT( year from ts ) as year, category, COUNT(category) FROM foo GROUP BY EXTRACT( year from ts ), category ORDER BY 3 DESC" Ok, how about using "year" instead of the extract( .. ) bit? sql> SELECT EXTRACT( year from ts ) as year, category, COUNT(category) FROM foo GROUP BY year, category ORDER BY 3 DESC; syntax error, unexpected YEAR in: "SELECT EXTRACT( year from ts ) as year, category, COUNT(category) FROM foo GROUP BY year, category ORDER BY 3 DESC" What is the correct way to group by the computed field? Should I create a view? Is there a positional syntax for the GROUP BY clause I am overlooking? $ monetdb --version MonetDB Database Server Toolkit v0.8 (Apr2011) $ monetdbd --version MonetDB Database Server v1.4 (Apr2011) $ mclient --version mclient, the MonetDB interactive terminal (Apr2011) support for command-line editing compiled-in Thank you.
On 01-06-2011 11:48:17 -0700, Synaptic Axon wrote:
Unfortunately, MonetDB does not appear to support positional fields in the GROUP BY clause. So I tried rewriting it like this:
sql> SELECT EXTRACT( year from ts ) as year, category, COUNT(category) FROM foo GROUP BY EXTRACT( year from ts ), category ORDER BY 3 DESC; syntax error, unexpected EXTRACT in: "SELECT EXTRACT( year from ts ) as year, category, COUNT(category) FROM foo GROUP BY EXTRACT( year from ts ), category ORDER BY 3 DESC"
Ok, how about using "year" instead of the extract( .. ) bit?
sql> SELECT EXTRACT( year from ts ) as year, category, COUNT(category) FROM foo GROUP BY year, category ORDER BY 3 DESC; syntax error, unexpected YEAR in: "SELECT EXTRACT( year from ts ) as year, category, COUNT(category) FROM foo GROUP BY year, category ORDER BY 3 DESC"
What is the correct way to group by the computed field? Should I create a view? Is there a positional syntax for the GROUP BY clause I am overlooking?
Niels knows exactly why this isn't allowed, but why do see this as an issue? I agree it clobbers the query, but that should be all...
On Wed, Jun 1, 2011 at 11:54 AM, Fabian Groffen
On 01-06-2011 11:48:17 -0700, Synaptic Axon wrote:
What is the correct way to group by the computed field? Should I create a view? Is there a positional syntax for the GROUP BY clause I am overlooking?
Niels knows exactly why this isn't allowed, but why do see this as an issue? I agree it clobbers the query, but that should be all...
Why do I see it as an issue? I was surprised that it was a syntax error and not supported. I would like to be able to execute aggregate queries against parts of a timestamp, to summarize over various periods of time or any number of other computed columns. I suppose one method would be to store the year as a separate column but that doesn't seem very flexible. I'm not using MonetDB in production but I'm evaluating it for that purpose. I was just surprised that I could not group by the computed year column and think I must be missing something regarding syntax. This isn't a common use case for you? Thank you.
On Wed, Jun 01, 2011 at 12:29:50PM -0700, Synaptic Axon wrote:
On Wed, Jun 1, 2011 at 11:54 AM, Fabian Groffen
wrote: On 01-06-2011 11:48:17 -0700, Synaptic Axon wrote:
What is the correct way to group by the computed field? Should I create a view? Is there a positional syntax for the GROUP BY clause I am overlooking?
Niels knows exactly why this isn't allowed, but why do see this as an issue? I agree it clobbers the query, but that should be all...
Why do I see it as an issue? I was surprised that it was a syntax error and not supported. I would like to be able to execute aggregate queries against parts of a timestamp, to summarize over various periods of time or any number of other computed columns.
I suppose one method would be to store the year as a separate column but that doesn't seem very flexible. I'm not using MonetDB in production but I'm evaluating it for that purpose. I was just surprised that I could not group by the computed year column and think I must be missing something regarding syntax. This isn't a common use case for you? Aliases should work just fine.
Niels
Thank you.
------------------------------------------------------------------------------ Simplify data backup and recovery for your virtual environment with vRanger. Installation's a snap, and flexible recovery options mean your data is safe, secure and there when you need it. Data protection magic? Nope - It's vRanger. Get your free trial download today. http://p.sf.net/sfu/quest-sfdev2dev _______________________________________________ MonetDB-users mailing list MonetDB-users@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/monetdb-users
-- Niels Nes, Centrum Wiskunde & Informatica (CWI) Science Park 123, 1098 XG Amsterdam, The Netherlands room L3.14, phone ++31 20 592-4098 sip:4098@sip.cwi.nl url: http://www.cwi.nl/~niels e-mail: Niels.Nes@cwi.nl
On Wed, Jun 01, 2011 at 08:54:19PM +0200, Fabian Groffen wrote:
On 01-06-2011 11:48:17 -0700, Synaptic Axon wrote:
Unfortunately, MonetDB does not appear to support positional fields in the GROUP BY clause. So I tried rewriting it like this:
sql> SELECT EXTRACT( year from ts ) as year, category, COUNT(category) FROM foo GROUP BY EXTRACT( year from ts ), category ORDER BY 3 DESC; syntax error, unexpected EXTRACT in: "SELECT EXTRACT( year from ts ) as year, category, COUNT(category) FROM foo GROUP BY EXTRACT( year from ts ), category ORDER BY 3 DESC"
Ok, how about using "year" instead of the extract( .. ) bit?
sql> SELECT EXTRACT( year from ts ) as year, category, COUNT(category) FROM foo GROUP BY year, category ORDER BY 3 DESC; syntax error, unexpected YEAR in: "SELECT EXTRACT( year from ts ) as year, category, COUNT(category) FROM foo GROUP BY year, category ORDER BY 3 DESC"
What is the correct way to group by the computed field? Should I create a view? Is there a positional syntax for the GROUP BY clause I am overlooking?
Niels knows exactly why this isn't allowed, but why do see this as an issue? I agree it clobbers the query, but that should be all...
Indeed we don't allow functions within the group by list (only column or alias references). Shouldn't be a big problem. In this case it goes wrong because 'year' is a keyword which needs double quotes if you want to use it as alias/ident. Niels
------------------------------------------------------------------------------ Simplify data backup and recovery for your virtual environment with vRanger. Installation's a snap, and flexible recovery options mean your data is safe, secure and there when you need it. Data protection magic? Nope - It's vRanger. Get your free trial download today. http://p.sf.net/sfu/quest-sfdev2dev _______________________________________________ MonetDB-users mailing list MonetDB-users@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/monetdb-users
-- Niels Nes, Centrum Wiskunde & Informatica (CWI) Science Park 123, 1098 XG Amsterdam, The Netherlands room L3.14, phone ++31 20 592-4098 sip:4098@sip.cwi.nl url: http://www.cwi.nl/~niels e-mail: Niels.Nes@cwi.nl
On Wed, Jun 1, 2011 at 12:31 PM, Niels Nes
Indeed we don't allow functions within the group by list (only column or alias references). Shouldn't be a big problem. In this case it goes wrong because 'year' is a keyword which needs double quotes if you want to use it as alias/ident.
Ah, I didn't recognize that 'year' was a keyword. Thanks! Seems to work fine now. sql>select extract( year from ts ) as myyear, category, count(category) from foo group by myyear, category order by 3 desc; +--------+----------+------+ | myyear | category | L7 | +========+==========+======+ | 2010 | bar | 1 | | 2011 | baz | 1 | +--------+----------+------+ 2 tuples (2.614ms)
participants (3)
-
Fabian Groffen
-
Niels Nes
-
Synaptic Axon