db-derby-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From <de...@segel.com>
Subject RE: How to emulate multiple DISTINCT aggregates?
Date Mon, 28 Aug 2006 18:00:37 GMT

Hi,

Uhm you can't do what you want to do as written....

When you say SELECT COUNT(DISTINCT blah), it has to be unique to your query.
So no multiple COUNT(DISTINCTS xxx) allowed in a single query.
However here's a work around that may help...

SELECT COUNT(a.field1), COUNT(a.field2), COUNT(a.field3) ...
FROM table_a a
WHERE  a.field1 in ( SELECT DISTINCT b.field1 FROM table_a b)
AND    a.field2 in ( SELECT DISTINCT b.field2 FROM table_a b)
AND    a.field3 in ( SELECT DISTINCT b.field3 FROM table_a b)
...

Well you get the idea.

It's then pretty straight forward.
Each subselect returns the distinct set of elements and then your outer
select returns the count on these unique subsets.

I just tried it in Informix (I happen to have  a table with a couple of
string columns of data that I am working on.)
Should work in Derby. Its all standard SQL

But Hey! What do I know? 

-G

> -----Original Message-----
> From: Robert Enyedi [mailto:robert.enyedi@intland.com]
> Sent: Monday, August 28, 2006 10:58 AM
> To: Derby Discussion
> Subject: How to emulate multiple DISTINCT aggregates?
> 
> Hi,
> 
> When trying to use multiple DISTINCT aggregates in Derby:
> 
> /SELECT COUNT(distinct editable), COUNT( distinct visible )
> FROM item/
> 
> the following error is reported:
> 
> /ERROR 42Z02: Multiple DISTINCT aggregates are not supported at this
> time./
> 
> For a simple query one could write:
> 
> /SELECT COUNT(distinct editable) AS editable_count, COUNT( (SELECT
> DISTINCT visible FROM item) )
> FROM item/
> 
> but this is actually pretty uncomfortable and unmaintainable in a larger
> query.
> 
> Does anyone know an elegant overcome for this Derby limitation?
> 
> Thanks,
> Robert



Mime
View raw message