Using DISTINCT with the COUNT Analytic Function

Moderator: NorbertKrupa

Post Reply
User avatar
Julie
Master
Master
Posts: 221
Joined: Thu Apr 19, 2012 9:29 pm

Using DISTINCT with the COUNT Analytic Function

Post by Julie » Fri Mar 14, 2014 12:57 pm

Howdy!

I'd like to get a count of a column's distinct values using the analytic function COUNT, but it doesn't work in Vertica:

Code: Select all

dbadmin=> select a, b, count(b) over (partition by a) from test;
 a | b | ?column? 
---+---+----------
 1 | 1 |        3
 1 | 1 |        3
 1 | 2 |        3
(3 rows)

dbadmin=> select a, b, count(distinct b) over (partition by a) from test;
ERROR 4249:  Only MIN/MAX are allowed to use DISTINCT
The "Only MIN/MAX are allowed to use DISTINCT" message from Vertica is kind of silly. What's the difference between MIN(DISTINCT b) and MIN(b)... nothing, right?

Anyway, I want this result:

Code: Select all

 a | b | ?column? 
---+---+----------
 1 | 1 |        2
 1 | 1 |        2
 1 | 2 |        1
The DISTINCT option is available in some Oracle code I have :x

Can anyone help with a work around?
Thanks,
Juliette

id10t
GURU
GURU
Posts: 732
Joined: Mon Apr 16, 2012 2:44 pm

Re: Using DISTINCT with the COUNT Analytic Function

Post by id10t » Fri Mar 14, 2014 1:54 pm

Hi!

[DELETED]
Last edited by id10t on Fri May 08, 2015 4:29 pm, edited 1 time in total.

NorbertKrupa
GURU
GURU
Posts: 527
Joined: Tue Oct 22, 2013 9:36 pm
Location: Chicago, IL
Contact:

Re: Using DISTINCT with the COUNT Analytic Function

Post by NorbertKrupa » Fri Mar 14, 2014 2:08 pm

Julie wrote:What's the difference between MIN(DISTINCT b) and MIN(b)... nothing, right?
There is no difference. The only use case I've seen is COUNT(DISTINCT col).

See the examples in this SQL Fiddle.
Checkout vertica.tips for more Vertica resources.

User avatar
Julie
Master
Master
Posts: 221
Joined: Thu Apr 19, 2012 9:29 pm

Re: Using DISTINCT with the COUNT Analytic Function

Post by Julie » Wed Mar 19, 2014 7:18 pm

Thanks, guys!
Thanks,
Juliette

Post Reply

Return to “Vertica SQL”