I have 2 tables without identical primary key between them (id
exist in both, primary only for table A
). I want to use the primary key of the first table A
for the ON
clause. Therefore I'll have duplicates from the second table B
. I want to GROUP BY
the duplicates based on some field B.cnt
and always take the first one - DESC LIMIT 1
.
This is what I tried (DBMS is PostgreSQL):
SELECT
scheme1.A.some_attr,
B.some_attr
FROM
(SELECT * FROM scheme2.B ORDER BY scheme2.B.cnt DESC LIMIT 1) AS B
INNER JOIN
scheme1.A
ON
scheme1.A.id = B.id
;
The query returns single record. While the desired behavior is to return single record just for each set of records from B
having same id
(based on the criteria mentioned). So in total the query of course will return multiple records...
How can I achieve the desired result?
Thanks,