0
votes

I am trying to use this query in Postgres:

SELECT ean, count(nif)
FROM produto P, fornece_sec F
WHERE P.ean = F.ean
GROUP BY ​P.ean
HAVING count(nif) > 1;

I get this error:

ERROR: column reference "ean" is ambiguous LINE 1: SELECT ean, count(nif)

If I change the query to:

SELECT P.ean, count(nif)
FROM produto P, fornece_sec F
WHERE P.ean = F.ean
GROUP BY ​P.ean
HAVING count(nif) > 1;

Appears another error:

ERROR: missing FROM-clause entry for table "​p" LINE 4: GROUP BY ​P.ean

Those two tables are created this way:

create table produto (
ean char(13) not null unique,
design varchar(255) not null,
categoria varchar(80) not null,
forn_primario char(9) not null,
data date not null,
constraint ean_size CHECK (Length(ean)=13),
constraint pk_produto primary key(ean),
constraint fk_produto_categoria foreign key(categoria) references categoria(nome) ON DELETE CASCADE,
constraint fk_produto_fornecedor foreign key(forn_primario) references fornecedor(nif));

create table fornece_sec (
nif char(9) not null,
ean char(13) not null,
constraint pk_fornece_sec primary key(nif, ean),
constraint fk_fornece_sec_fornecedor foreign key(nif) references fornecedor(nif),
constraint fk_fornece_sec_produto foreign key(ean) references produto(ean) ON DELETE CASCADE);

Note: the attribute nif in fornece_sec table is the same type of the attribute forn_primario in the product table.

2
Your GROUP BY seems to contain unicode, not ASCII. Maybe the P is non-ASCII character that looks like P. - tonypdmtr
You've tagged sql-server, postgresql and mysql here. They are all different products. Which are you using..? - Larnu
Please tag properly. MySQL <> Postgres <> SQL Server - Eric
Also use proper JOIN - Eric

2 Answers

0
votes

This probably has nothing to do with it, but maybe try explicitly declaring the INNER JOIN:

SELECT P.ean, count(nif)
FROM produto P
INNER JOIN fornece_sec F ON P.ean = F.ean
GROUP BY ​P.ean
HAVING count(nif) > 1;
0
votes

Tommy was right:

"Your GROUP BY seems to contain unicode, not ASCII. Maybe the P is non-ASCII character that looks like P."