Re: where not unique

From: Daniel Henrique Alves Lima <email_daniel_h(at)yahoo(dot)com(dot)br>
To: Gary Stainburn <gary(dot)stainburn(at)ringways(dot)co(dot)uk>
Cc: pgsql-sql(at)postgresql(dot)org
Subject: Re: where not unique
Date: 2004-03-12 13:54:25
Message-ID: 4051C111.40301@yahoo.com.br
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

Gary Stainburn wrote:

>Hi folks,
>
>I've got a table which contains amongst other things a stock number and a
>registration number. I need to print out a list of stock number and reg
>number where reg number is not unique (cherished number plate transfer not
>completed). I've tried variations of a theme based on
>
>select stock_number, registration from stock where registration in
> (select registration, count(registration) as count
> from stock where count > 1 group by registration);
>
>but I have two problems. Firstly with the sub-select I get:
>
>usedcars=# select registration, count(registration) as count from stock where
>count > 1 group by registration;
>ERROR: Attribute 'count' not found
>usedcars=#
>
>although if I miss out the where clause I get the expected results.
>
I think that this is not the best way, but :

select * from (select registration, count(registration) as counter from
stock group by registration)
where counter > 1;

>
>Secondly, when I run the full query I get:
>
>usedcars=# select stock_number, registration from stock
>usedcars-# where registration in
>usedcars-# (select registration, count(registration) as count from stock group
>by registration);
>ERROR: Subselect has too many fields
>usedcars=#
>
>
This is because the subselect is returning 2 columns but "in clause"
is expecting just one column.
Try to use "exists" instead of "in", ok ?

>which is obviously because of the count field.
>
>Can anyone tell me where I'm going wroing with these count fields?
>(I've tried renaming the field to regcount in case it was a reserved word
>problem).
>
>

I hope this helps you.

In response to

Responses

Browse pgsql-sql by date

  From Date Subject
Next Message Paul Thomas 2004-03-12 13:57:00 Re: where not unique
Previous Message Gary Stainburn 2004-03-12 13:02:09 where not unique